From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00 autolearn=unavailable autolearn_force=no version=3.4.4 Path: eternal-september.org!reader01.eternal-september.org!.POSTED!not-for-mail From: Simon Wright Newsgroups: comp.lang.ada Subject: Re: type definition for an integer with discrete range Date: Fri, 29 Mar 2019 21:24:53 +0000 Organization: A noiseless patient Spider Message-ID: References: <36c2ff61-8cca-4435-995f-dfc34fa44b69@googlegroups.com> <511923fb-dbb9-4e33-82a8-b4bbbf002a6d@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: reader02.eternal-september.org; posting-host="d3edd0845bf91e08b6bbdb3ecf0226de"; logging-data="29595"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18Xz7R8oan3cf0wCChkLNdaqvL6Ryma+kQ=" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (darwin) Cancel-Lock: sha1:ww2EJ2J2SBniToy2dbaOR9jn2P8= sha1:a1Ka4kA+4L0xZj5gkkB21Dt2mxA= Xref: reader01.eternal-september.org comp.lang.ada:56006 Date: 2019-03-29T21:24:53+00:00 List-Id: mario.blunk.gplus@gmail.com writes: > On Friday, March 29, 2019 at 9:24:38 PM UTC+1, Simon Wright wrote: >> tra.....g@gmail.com writes: >> >> > On Friday, March 29, 2019 at 11:10:42 PM UTC+7, mario.b...@gmail.com wrote: >> >> Hello, >> >> I'm looking for a way to define a type that runs from let say -100 >> >> to +100 with gaps of 5 width. Important is to make sure that a value >> >> like 7 can not be assigned to the type. >> >> >> >> something like: >> >> >> >> type number is new integer range -100 .. 100; >> >> -- or >> >> subtype number is integer range -100 .. 100; >> >> >> >> -- with this special thing or something like that: >> >> for number'small use 5; -- can not applied here. works with fixed >> >> point types only >> >> >> >> Thanks ! [...] >> Number_Small : constant := 5.0; >> type Number is delta Number_Small range -100.0 .. 100.0; >> for Number'Small use Number_Small; > > The problem is that you can assign a variable of type Number 101.0 > without getting a compile error. I wrote a test program at > https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb > where the issue can be tested by yourself. Thanks ! If you compile with -gnatwa the compiler says type_angle.adb:11:22: warning: static fixed-point value is not a multiple of Small type_angle.adb:14:13: warning: static fixed-point value is not a multiple of Small type_angle.adb:18:13: warning: value not in range of type "Number" defined at line 8 type_angle.adb:18:13: warning: "Constraint_Error" will be raised at run time Changing your 101.0 to 104.9 gives the same result. Printing the "101.0" value gives 100.0 - so the constraint hasn't been violated. What about this? pragma Assertion_Policy (Check); with Ada.Text_Io; use Ada.Text_Io; procedure Type_Integer is subtype Number is Integer range -100 .. 100 with Dynamic_Predicate => Number mod 5 = 0; V : Number; begin V := 0; Put_Line ("0'image is " & V'Image); V := -50; Put_Line ("-50'image is " & V'Image); V := 42; Put_Line ("42'image is " & V'Image); end Type_Integer; Executing gives $ ./type_integer 0'image is 0 -50'image is -50 raised SYSTEM.ASSERTIONS.ASSERT_FAILURE : Dynamic_Predicate failed at type_integer.adb:12