* type definition for an integer with discrete range @ 2019-03-29 16:10 mario.blunk.gplus 2019-03-29 16:34 ` tranngocduong 2019-03-30 21:45 ` John Perry 0 siblings, 2 replies; 16+ messages in thread From: mario.blunk.gplus @ 2019-03-29 16:10 UTC (permalink / raw) 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 ! ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 16:10 type definition for an integer with discrete range mario.blunk.gplus @ 2019-03-29 16:34 ` tranngocduong 2019-03-29 16:46 ` mario.blunk.gplus 2019-03-29 20:24 ` Simon Wright 2019-03-30 21:45 ` John Perry 1 sibling, 2 replies; 16+ messages in thread From: tranngocduong @ 2019-03-29 16:34 UTC (permalink / raw) 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 ! That's a fixed point type. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 16:34 ` tranngocduong @ 2019-03-29 16:46 ` mario.blunk.gplus 2019-03-30 4:17 ` tranngocduong 2019-03-29 20:24 ` Simon Wright 1 sibling, 1 reply; 16+ messages in thread From: mario.blunk.gplus @ 2019-03-29 16:46 UTC (permalink / raw) On Friday, March 29, 2019 at 5:34:24 PM UTC+1, tranng...@gmail.com wrote: > 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 ! > > That's a fixed point type. It is about angles to be correct. The problem is that fixed types are rounded. I tried type type_angle is delta 90.0 range -359.9 .. 359.9; -- unit is degrees for type_angle'small use 90.0; -- but it is possible to assign a value that is not a multiple of 90 degree and even outside the domain of type_angle: angle : type_angle := 370.0; I want a constraint error being raised. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 16:46 ` mario.blunk.gplus @ 2019-03-30 4:17 ` tranngocduong 2019-03-30 4:19 ` tranngocduong 0 siblings, 1 reply; 16+ messages in thread From: tranngocduong @ 2019-03-30 4:17 UTC (permalink / raw) On Friday, March 29, 2019 at 11:46:48 PM UTC+7, mario.b...@gmail.com wrote: > On Friday, March 29, 2019 at 5:34:24 PM UTC+1, tranng...@gmail.com wrote: > > 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 ! > > > > That's a fixed point type. > > It is about angles to be correct. The problem is that fixed types are rounded. I tried > > type type_angle is delta 90.0 range -359.9 .. 359.9; -- unit is degrees > for type_angle'small use 90.0; > > -- but it is possible to assign a value that is not a multiple of 90 degree and even outside the domain of type_angle: > angle : type_angle := 370.0; > > I want a constraint error being raised. Actually, the domain is approximately [-450.0,+450.0], as pointed out by D. Kazakov. If you need the domain closer to [-360.0,+360.0], use smaller number'small. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-30 4:17 ` tranngocduong @ 2019-03-30 4:19 ` tranngocduong 0 siblings, 0 replies; 16+ messages in thread From: tranngocduong @ 2019-03-30 4:19 UTC (permalink / raw) > If you need the domain closer to [-360.0,+360.0], use smaller number'small. I mean type_angle'small. Sorry for mistyping. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 16:34 ` tranngocduong 2019-03-29 16:46 ` mario.blunk.gplus @ 2019-03-29 20:24 ` Simon Wright 2019-03-29 20:51 ` mario.blunk.gplus 1 sibling, 1 reply; 16+ messages in thread From: Simon Wright @ 2019-03-29 20:24 UTC (permalink / raw) tranngocduong@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 ! > > That's a fixed point type. In Ada, that's an integer type. You can tell a real type because its literals *must* have a decimal point in them. Fixed- and floating-point types are both real types. Mario's code might look like Number_Small : constant := 5.0; type Number is delta Number_Small range -100.0 .. 100.0; for Number'Small use Number_Small; ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 20:24 ` Simon Wright @ 2019-03-29 20:51 ` mario.blunk.gplus 2019-03-29 21:24 ` Simon Wright 2019-03-29 21:57 ` Dmitry A. Kazakov 0 siblings, 2 replies; 16+ messages in thread From: mario.blunk.gplus @ 2019-03-29 20:51 UTC (permalink / raw) 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 ! > > > > That's a fixed point type. > > In Ada, that's an integer type. You can tell a real type because its > literals *must* have a decimal point in them. Fixed- and floating-point > types are both real types. > > Mario's code might look like > > 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 ! ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 20:51 ` mario.blunk.gplus @ 2019-03-29 21:24 ` Simon Wright 2019-03-30 20:44 ` mario.blunk.gplus 2019-03-29 21:57 ` Dmitry A. Kazakov 1 sibling, 1 reply; 16+ messages in thread From: Simon Wright @ 2019-03-29 21:24 UTC (permalink / raw) 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 ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 21:24 ` Simon Wright @ 2019-03-30 20:44 ` mario.blunk.gplus 2019-03-30 22:13 ` Jere 0 siblings, 1 reply; 16+ messages in thread From: mario.blunk.gplus @ 2019-03-30 20:44 UTC (permalink / raw) On Friday, March 29, 2019 at 10:24:55 PM UTC+1, Simon Wright wrote: > > 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 Great, that brings me a lot further. I updated the file at https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb Two more questions: 1. How can I catch the exception SYSTEM.ASSERTIONS.ASSERT_FAILURE in the exception handler ? See line 26. 2. How can I print the "gap size" via put ? See line 28. Thanks to all of you out there. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-30 20:44 ` mario.blunk.gplus @ 2019-03-30 22:13 ` Jere 2019-04-01 6:59 ` mario.blunk.gplus 0 siblings, 1 reply; 16+ messages in thread From: Jere @ 2019-03-30 22:13 UTC (permalink / raw) On Saturday, March 30, 2019 at 4:44:07 PM UTC-4, mario.b...@gmail.com wrote: > Great, that brings me a lot further. I updated the file at > https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb > Two more questions: > 1. How can I catch the exception SYSTEM.ASSERTIONS.ASSERT_FAILURE in the exception handler ? See line 26. > 2. How can I print the "gap size" via put ? See line 28. > > Thanks to all of you out there. 1. Add a "with" for System.Assertions, uncomment line 26, add your handling code. 2. Declare a constant: Angle_Delta : constant := 5; Use it in your Dynamic_Predicate and then in your print statement, do Integer'Image(Angle_Delta); ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-30 22:13 ` Jere @ 2019-04-01 6:59 ` mario.blunk.gplus 2019-04-01 15:52 ` AdaMagica 0 siblings, 1 reply; 16+ messages in thread From: mario.blunk.gplus @ 2019-04-01 6:59 UTC (permalink / raw) On Saturday, March 30, 2019 at 11:13:14 PM UTC+1, Jere wrote: > On Saturday, March 30, 2019 at 4:44:07 PM UTC-4, mario.b...@gmail.com wrote: > > Great, that brings me a lot further. I updated the file at > > https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb > > Two more questions: > > 1. How can I catch the exception SYSTEM.ASSERTIONS.ASSERT_FAILURE in the exception handler ? See line 26. > > 2. How can I print the "gap size" via put ? See line 28. > > > > Thanks to all of you out there. > > 1. Add a "with" for System.Assertions, uncomment line 26, add your > handling code. > > 2. Declare a constant: > Angle_Delta : constant := 5; > Use it in your Dynamic_Predicate and then in your print statement, do > Integer'Image(Angle_Delta); Thanks, it is nearly perfect now :-) The line 24 in the file https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb Should produce an error at compile time. Apart from this "nice to have" the program does what I want. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-04-01 6:59 ` mario.blunk.gplus @ 2019-04-01 15:52 ` AdaMagica 2019-04-01 16:27 ` Simon Wright 0 siblings, 1 reply; 16+ messages in thread From: AdaMagica @ 2019-04-01 15:52 UTC (permalink / raw) Am Montag, 1. April 2019 08:59:56 UTC+2 schrieb mario.b...@gmail.com: > Thanks, it is nearly perfect now :-) > The line 24 in the file > https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb > Should produce an error at compile time. Apart from this "nice to have" the program does what I want. You have to make sure that "pragma Assertion_Policy (Check);" is set. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-04-01 15:52 ` AdaMagica @ 2019-04-01 16:27 ` Simon Wright 2019-04-01 16:41 ` AdaMagica 0 siblings, 1 reply; 16+ messages in thread From: Simon Wright @ 2019-04-01 16:27 UTC (permalink / raw) AdaMagica <christ-usch.grein@t-online.de> writes: > Am Montag, 1. April 2019 08:59:56 UTC+2 schrieb mario.b...@gmail.com: >> Thanks, it is nearly perfect now :-) >> The line 24 in the file >> https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb >> Should produce an error at compile time. Apart from this "nice to >> have" the program does what I want. > > You have to make sure that "pragma Assertion_Policy (Check);" is set. Yes, that's why it appears at the top of the program. Alternatively you could compile with -gnata. If I have the Assertion_Policy pragma in spec A, and package B uses that spec, are the assertions specified in A applied in B? (I suspect not) ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-04-01 16:27 ` Simon Wright @ 2019-04-01 16:41 ` AdaMagica 0 siblings, 0 replies; 16+ messages in thread From: AdaMagica @ 2019-04-01 16:41 UTC (permalink / raw) Am Montag, 1. April 2019 18:27:32 UTC+2 schrieb Simon Wright: > AdaMagica <christ-usch.grein@t-online.de> writes: > > > Am Montag, 1. April 2019 08:59:56 UTC+2 schrieb mario.b...@gmail.com: > >> Thanks, it is nearly perfect now :-) > >> The line 24 in the file > >> https://github.com/Blunk-electronic/ada_training/blob/master/src/type_angle/type_angle.adb > >> Should produce an error at compile time. Apart from this "nice to > >> have" the program does what I want. > > > > You have to make sure that "pragma Assertion_Policy (Check);" is set. > > Yes, that's why it appears at the top of the program. Oh, I missed this. > Alternatively you could compile with -gnata. > > If I have the Assertion_Policy pragma in spec A, and package B uses that > spec, are the assertions specified in A applied in B? (I suspect not) You're right with your suspicion. That's why this pragma is a "configuration pragma"; it should be in a special file (compiler dependent), then it applies to all units to which this config file applies. Or, for GNAT only, you use -gnata, as you said above. ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 20:51 ` mario.blunk.gplus 2019-03-29 21:24 ` Simon Wright @ 2019-03-29 21:57 ` Dmitry A. Kazakov 1 sibling, 0 replies; 16+ messages in thread From: Dmitry A. Kazakov @ 2019-03-29 21:57 UTC (permalink / raw) On 2019-03-29 21:51, mario.blunk.gplus@gmail.com wrote: > 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 ! Values of a fixed types are intervals with real bounds. Each interval has a machine representation by an integer number. That means that for each fixed value there is a whole set (infinite) of real numbers equivalent to it. When you specify a range like type Number is delta Number_Small range -100.0 .. 100.0; that does not mean that upper bound of the last interval is 100.0. It only means that the last interval contains or is bounded by 100.0 (see ARM 3.5.9(13)). You could take any real value from the interval with the same effect. To illustrate the point try this program: ------------------------------------- with Ada.Text_IO; use Ada.Text_IO; procedure type_angle is Number_Small : constant := 5.0; type Number is delta Number_Small range -100.0 .. 100.0; angle : number; begin angle := -4.0; Put_Line (" -4.0 =" & Number'Image (angle)); angle := -3.0; Put_Line (" -3.0 =" & Number'Image (angle)); angle := -2.0; Put_Line (" -2.0 =" & Number'Image (angle)); angle := -1.0; Put_Line (" -1.0 =" & Number'Image (angle)); angle := 0.0; Put_Line (" 0.0 =" & Number'Image (angle)); angle := 1.0; Put_Line (" 1.0 =" & Number'Image (angle)); angle := 2.0; Put_Line (" 2.0 =" & Number'Image (angle)); angle := 3.0; Put_Line (" 3.0 =" & Number'Image (angle)); angle := 4.0; Put_Line (" 4.0 =" & Number'Image (angle)); angle := 100.0; Put_Line ("100.0 =" & Number'Image (angle)); angle := 101.0; Put_Line ("101.0 =" & Number'Image (angle)); angle := 102.0; Put_Line ("102.0 =" & Number'Image (angle)); angle := 103.0; Put_Line ("103.0 =" & Number'Image (angle)); angle := 103.9; Put_Line ("103.9 =" & Number'Image (angle)); end type_angle; ------------------------------------- On my machine it prints: -4.0 =-4.0 -3.0 = 0.0 -2.0 = 0.0 -1.0 = 0.0 0.0 = 0.0 1.0 = 0.0 2.0 = 0.0 3.0 = 0.0 4.0 = 4.0 100.0 = 100.0 101.0 = 100.0 102.0 = 100.0 103.0 = 100.0 103.9 = 100.0 I.e. the last value of the last interval is in fact 103.9999(9) > 100.0. It is upper bound is 104. I.e. the interval in my case is half-open: [100, 104[ Note also that ARM requires symmetric representation around 0 (ARM 3.5.9(12)) which makes it 2 x Small wide! It might look strange, but otherwise -(-X) /= X. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de ^ permalink raw reply [flat|nested] 16+ messages in thread
* Re: type definition for an integer with discrete range 2019-03-29 16:10 type definition for an integer with discrete range mario.blunk.gplus 2019-03-29 16:34 ` tranngocduong @ 2019-03-30 21:45 ` John Perry 1 sibling, 0 replies; 16+ messages in thread From: John Perry @ 2019-03-30 21:45 UTC (permalink / raw) You wanted a compile-time error; would a warning do? I only have it from -20 to 20 for the sake of time & space, but a Static_Predicate does the trick. {{{ pragma Assertion_Policy(Check); procedure Main is subtype Number is Integer with Static_Predicate => Number in -20 | -15 | -10 | -5 | 0 | 5 | 10 | 15 | 20; A, B: Number; begin A := 10; B := 12; end Main; }}} ^ permalink raw reply [flat|nested] 16+ messages in thread
end of thread, other threads:[~2019-04-01 16:41 UTC | newest] Thread overview: 16+ messages (download: mbox.gz / follow: Atom feed) -- links below jump to the message on this page -- 2019-03-29 16:10 type definition for an integer with discrete range mario.blunk.gplus 2019-03-29 16:34 ` tranngocduong 2019-03-29 16:46 ` mario.blunk.gplus 2019-03-30 4:17 ` tranngocduong 2019-03-30 4:19 ` tranngocduong 2019-03-29 20:24 ` Simon Wright 2019-03-29 20:51 ` mario.blunk.gplus 2019-03-29 21:24 ` Simon Wright 2019-03-30 20:44 ` mario.blunk.gplus 2019-03-30 22:13 ` Jere 2019-04-01 6:59 ` mario.blunk.gplus 2019-04-01 15:52 ` AdaMagica 2019-04-01 16:27 ` Simon Wright 2019-04-01 16:41 ` AdaMagica 2019-03-29 21:57 ` Dmitry A. Kazakov 2019-03-30 21:45 ` John Perry
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox