Previous topic | Ada Home Page | Index

Example program

The following program demonstrates several things:

The last two are the main points of the example.

Example program



-- demonstrate rounding errors                               

with TEXT_IO;  
procedure roundoff is

   package float_io is new TEXT_IO.FLOAT_IO( FLOAT );
   TEXT_IO, float_io;

   zero               : constant       := 0.0;
   one                : constant float := 1.0;
   ten_thousand       : constant       := 10000;
   one_ten_thousandth : constant FLOAT 
                        := one / FLOAT(ten_thousand);           
   the_result         : FLOAT := zero;

begin -- roundoff

   -- the_result is initially equal to zero
   for counter in INTEGER range 1 .. ten_thousand loop
      the_result := the_result + one_ten_thousandth;
   end loop;

   -- mathematically, the_result is now equal to 
   --      1/10_000 * 10_000 = 1.0

   if (the_result = one) then
      PUT_LINE("Isn't this what you expected!");
   else
      PUT_LINE("Isn't this a surprise!");
      PUT("One        is "); PUT(one, EXP => 0); NEW_LINE;
      PUT("The result is "); PUT(the_result, EXP=>0);
      NEW_LINE;
   end if;
end roundoff;

Example output


        Isn't this a surprise!
        One        is  1.00000000000000
        The result is  0.99999999999991


Previous topic | Ada Home Page | Index
c-lokan@adfa.oz.au / 12 Feb 96