comp.lang.ada
 help / color / mirror / Atom feed
From: Adam Beneschan <adam@irvine.com>
Subject: Re: float confusion
Date: Tue, 14 Jul 2009 17:18:58 -0700 (PDT)
Date: 2009-07-14T17:18:58-07:00	[thread overview]
Message-ID: <6aa1a40c-641f-42d5-8c93-6bd6898873b8@b14g2000yqd.googlegroups.com> (raw)
In-Reply-To: 0u6q55lvsmo3urasars18pti1g4tcajqek@4ax.com

On Jul 14, 5:03 pm, Rob Solomon <use...@drrob1-noreply.com> wrote:
> On Tue, 14 Jul 2009 16:50:20 -0700 (PDT), Adam Beneschan
>
>
>
>
>
> <a...@irvine.com> wrote:
> >> 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);
> >>     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;
>
> >> And interestingly, I get overflow errors from the *2 statement but not
> >> from self+self.
>
> >How can you tell?  Your above example gives you no way to tell what
> >line you get an overflow on.  And the way it's written, if you got an
> >overflow on X := X*2, the exception would take you out of the loop and
> >you'd never get to the p := p+p statement for the same value (since it
> >looks like X=n=p will always be true at the beginning of the loop).
>
> Ok.  I got this example from the online tutorial and I kept modifying
> it to try something new.  Before I experimented w/  exception
> handling, I was getting constraint_error exceptions.  
>
> But Jeff Carter above explained that was I was seeing was really range
> checking effects and not overflow effects.  That helped me understand
> what was going on

Well, maybe.  Jeff said that Natural and Positive have range
constraints that Integer does not; however, those constraints prevent
objects from having negative values (or zero, for Positive), and since
your program is working only with positive numbers, that shouldn't
have had an effect.  However, if you don't have overflow checking on (-
gnato), then what happens is that when you get to the largest possible
power of 2, which is 2**30 for a 32-bit integer, and then double it,
the result will be seen as negative (-2**31), which (without overflow
checking) will cause a Constraint_Error for a Natural or Positive but
no exception at all for an Integer.  So in your example, this would
mean "n:=n*2" and "p:=p+p" would have raised exceptions, but "X:=X*2"
would not.  So I'm still not clear on why you'd think this is a
difference between multiplication and addition, unless you were
experimenting with different code earlier.

                                   -- Adam




      reply	other threads:[~2009-07-15  0:18 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
2009-07-14 23:50 ` Adam Beneschan
2009-07-15  0:03   ` Rob Solomon
2009-07-15  0:18     ` Adam Beneschan [this message]
replies disabled

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