comp.lang.ada
 help / color / mirror / Atom feed
* float confusion
@ 2009-07-14 22:46 Rob Solomon
  2009-07-14 23:23 ` Jeffrey R. Carter
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Rob Solomon @ 2009-07-14 22:46 UTC (permalink / raw)


I ran this simple example thru GNAT and I'm confused by the result.

-- conclusions from this pgm
-- integer type does not raise overflow constraint_error
-- natural and positive types do raise overflow constraint_error when
-- overflow is by multiplication, but not by addition.

with Ada.Text_IO;
with Ada.Integer_Text_IO;
with Ada.Float_Text_IO;
use Ada.Text_IO;
use Ada.Integer_Text_IO;
use Ada.Float_Text_IO;


   procedure SimpleCompute is

   X : Integer := 1;
   c : Integer := 0;  -- counter
   N : Natural := 1;
   P : Positive := 1;
   F : Float := 1.0;

   begin
   put_line("X C N P F");
   loop
    put(X);
    card32_IO.put(u2);
    put(c,3);
    put(n);
    put(' ');
    put(p);
    put(' ');
    put(f,15,0,0);
    New_Line;
    X := X * 2;
    n := n * 2;
    p := p + p;
    f := f * 2.0;
    c := c +1;
    exit when c > 40;
   end loop;
   exception
      when constraint_error =>
         n := 1;
   end simplecompute;

My confusion is that float'digits is 6 and long_float'digits is 15 (I
already checked this).

When I run this code, float grows to the full 13 digits before I stop
it.  Why is that?

And interestingly, I get overflow errors from the *2 statement but not
from self+self.  I wonder if this occurs because this kind of overflow
is needed in the computation of random numbers.

Thx



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

end of thread, other threads:[~2009-07-15  0:18 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-07-14 22:46 float confusion Rob Solomon
2009-07-14 23:23 ` Jeffrey R. Carter
2009-07-14 23:26 ` Randy Brukardt
2009-07-14 23:50 ` Adam Beneschan
2009-07-15  0:03   ` Rob Solomon
2009-07-15  0:18     ` Adam Beneschan

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