comp.lang.ada
 help / color / mirror / Atom feed
From: JP Thornley <jpt@diphi.demon.co.uk>
Subject: Re: Disturbing float conversions...
Date: 2000/05/02
Date: 2000-05-02T00:00:00+00:00	[thread overview]
Message-ID: <Cd$54CA4IzD5IwsL@diphi.demon.co.uk> (raw)
In-Reply-To: 8en7ij$kr7$1@nnrp1.deja.com

In article <8en7ij$kr7$1@nnrp1.deja.com>, jboulais@my-deja.com writes
>I'm seeing something similar, but perhaps a little more disturbing
>(Gnat 3.11p and WinNT 4.0).

With Gnat 3.11p and Win98SE on a PIII the answers look a lot more like
you would expect:

>
>I've got the following types defined:
>
>   type Long_Float_Type is digits 8 range -32768.000 .. 32767.000 ;
>
>   subtype Angle_Type is Long_Float_Type range -180.00000 .. 179.99451 ;
>
>I find that when I do a 'Image on the 'First and 'Last of Angle_Type, I
>get -1.7999872E+02 and 1.7999872E+02 respectively.

I get:
-1.8000000E+02
 1.7999451E+02

>If I convert to a long_float (Ada standard, not Long_Float_Type) I get
>-1.80000000000000E+02 and 1.79994510000127E+02 respectively, which is
>closer, but the 'Last is a little bigger than I defined it to.

I get:
-1.80000000000000E+02
 1.79994510000000E+02

But, of course, 179.99451 cannot be represented *exactly* in any binary
floating point value (the fractional part doesn't end in a decimal '5').

>If I instantiate Text_IO.Float_IO with Angle_Type, I get -204.7 and
>204.7, which really blows me away.
now that is bizarre, I get:
-1.8000000E+02
 1.7999451E+02

Phil Thornley

My code is:

with Ada.Text_IO;
procedure Float_Values is

   type Long_Float_Type is digits 8 range -32768.000 .. 32767.000 ;

   subtype Angle_Type is Long_Float_Type range -180.00000 .. 179.99451 ;
   
   package AT_IO is new Ada.Text_IO.Float_IO(Angle_Type);
   
begin
   Ada.Text_IO.Put_Line(Long_Float_Type'Image(Angle_Type'First));
   Ada.Text_IO.Put_Line(Long_Float_Type'Image(Angle_Type'Last));

   Ada.Text_IO.Put_Line(Long_Float'Image(Long_Float(Angle_Type'First)));
   Ada.Text_IO.Put_Line(Long_Float'Image(Long_Float(Angle_Type'Last)));
   
   AT_IO.Put(Angle_Type'First);
   Ada.Text_IO.New_Line;
   AT_IO.Put(Angle_Type'Last);
   Ada.Text_IO.New_Line;
   
end Float_Values;
-- 
JP Thornley




  reply	other threads:[~2000-05-02  0:00 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-04-17  0:00 Assignment of small Float values David W. Glessner
2000-04-29  0:00 ` swhalen
2000-04-30  0:00   ` Robert I. Eachus
2000-05-02  0:00 ` Disturbing float conversions jboulais
2000-05-02  0:00   ` JP Thornley [this message]
2000-05-11  0:00     ` jboulais
2000-05-13  0:00       ` JP Thornley
replies disabled

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