comp.lang.ada
 help / color / mirror / Atom feed
* When floating point result intervals span the boundary - what value is selected?
@ 2006-03-03  1:30 Justin Gombos
  2006-03-03  2:55 ` haley
  2006-03-03  5:51 ` Jeffrey R. Carter
  0 siblings, 2 replies; 6+ messages in thread
From: Justin Gombos @ 2006-03-03  1:30 UTC (permalink / raw)


Question for the language lawyers-

Suppose you have the type:

   type Sine_Type is digits 6 range -1.0..1.0;

You perform an operation on two objects of this type, and the
mathematical result is exactly 1.0, so the result interval is
/partially/ in range.  Is there any guarantee that the stored result
will be in range?

It would be unreasonable for a compiler to select the next highest
model number, but it seems the language allows it.  The Ada LRM
<http://www.adaic.org/standards/95lrm/html/RM-G-2-1.html> seems to
make no guarantees - leaving it up to the compiler (whenever
T'Machine_Overflows is false).  The GNAT manual doesn't state either
way what it does.

This code raises some more questions:

   with Ada.Text_IO;

   procedure Tiny_Real is

      type Sine_Type is digits 6 range -1.0..1.0;

      Oversized : Sine_Type := Sine_Type'Last + Sine_Type'Small;

      Well_Oversized : constant Float := Float(Sine_Type'Last) + Float(Sine_Type'Small * 500000.0);

   begin

      Ada.Text_IO.Put ("The smallest possible float is " & Float'Image(Float'Small));
      Ada.Text_IO.New_Line;

      -- Strangely, the smallest possible float can be reduced even more.
      -- Could it be that the value is in a register, thus capable of
      -- representing fractional model number sizes?
      --
      Ada.Text_IO.Put ("A quarter of that is " & Float'Image(Float'Small / 4.0));
      Ada.Text_IO.New_Line;

      -- I was expecting this to report a model number close to 1.0, not 3.40282E+38.
      --
      Ada.Text_IO.Put ("Safe_Last is " & Float'Image(Sine_Type'Safe_Last));
      Ada.Text_IO.New_Line;

      Ada.Text_IO.Put ("Oversized is " & Float'Image(Float(Oversized)));

      -- I was expecting this to throw a constraint error:
      --
      Oversized := Sine_Type(Well_Oversized);

      Ada.Text_IO.New_Line;
      Ada.Text_IO.Put ("Oversized is " & Float'Image(Float(Oversized)));

   end Tiny_Real;

The output:

   The smallest possible float is  2.58494E-26
   A quarter of that is  6.46235E-27
   Safe_Last is  3.40282E+38
   Oversized is  1.00000E+00
   Oversized is  1.00000E+00

-- 
PM instructions: do a C4esar Ciph3r on my address; retain punctuation.



^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2006-03-05 19:13 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2006-03-03  1:30 When floating point result intervals span the boundary - what value is selected? Justin Gombos
2006-03-03  2:55 ` haley
2006-03-03  5:51 ` Jeffrey R. Carter
2006-03-04 16:46   ` Justin Gombos
2006-03-05 19:12     ` Jeff Carter
2006-03-05 19:13     ` Jeff Carter

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