comp.lang.ada
 help / color / mirror / Atom feed
From: Rob Solomon <usenet@drrob1-noreply.com>
Subject: float confusion
Date: Tue, 14 Jul 2009 18:46:01 -0400
Date: 2009-07-14T18:46:01-04:00	[thread overview]
Message-ID: <sc2q55185rv4dhc8dk0feu0dvuq7887oul@4ax.com> (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



             reply	other threads:[~2009-07-14 22:46 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-07-14 22:46 Rob Solomon [this message]
2009-07-14 23:23 ` float confusion 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
replies disabled

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