comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: type definition for an integer with discrete range
Date: Fri, 29 Mar 2019 22:57:52 +0100
Date: 2019-03-29T22:57:52+01:00	[thread overview]
Message-ID: <q7m4cu$llp$1@gioia.aioe.org> (raw)
In-Reply-To: 511923fb-dbb9-4e33-82a8-b4bbbf002a6d@googlegroups.com

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


  parent reply	other threads:[~2019-03-29 21:57 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2019-03-30 21:45 ` John Perry
replies disabled

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox