comp.lang.ada
 help / color / mirror / Atom feed
From: "Randy Brukardt" <randy@rrsoftware.com>
Subject: Re: float confusion
Date: Tue, 14 Jul 2009 18:26:19 -0500
Date: 2009-07-14T18:26:19-05:00	[thread overview]
Message-ID: <h3j48f$g2a$1@munin.nbi.dk> (raw)
In-Reply-To: sc2q55185rv4dhc8dk0feu0dvuq7887oul@4ax.com

Did you use -gnato? Gnat doesn't check integer overflow by default (one of 
the worst design decisions in GNAT, IMHO.)

If you are just starting out, you ought to use -gnato -fstack_check on all 
of your compilations in order to get the behavior specified by the standard. 
(To be perfectly like the Ada Standard, you also need to use -gnatE, but 
that is much less likely to cause trouble.)

                                    Randy.

"Rob Solomon" <usenet@drrob1-noreply.com> wrote in message 
news:sc2q55185rv4dhc8dk0feu0dvuq7887oul@4ax.com...
>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 





  parent reply	other threads:[~2009-07-14 23:26 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-14 22:46 float confusion Rob Solomon
2009-07-14 23:23 ` Jeffrey R. Carter
2009-07-14 23:26 ` Randy Brukardt [this message]
2009-07-14 23:50 ` Adam Beneschan
2009-07-15  0:03   ` Rob Solomon
2009-07-15  0:18     ` Adam Beneschan
replies disabled

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