comp.lang.ada
 help / color / mirror / Atom feed
From: Nicolas Brunot <n.brunot@cadwin.com>
Subject: Re: Why no new posts ? Float accuracy
Date: 2000/11/15
Date: 2000-11-15T00:00:00+00:00	[thread overview]
Message-ID: <3A125C2A.2E3BD2F@cadwin.com> (raw)
In-Reply-To: 002001c04ec8$27db8ee0$b0375140@Fudge

This looks like a typical accuracy problem with floating point types.
You cannot reasonably expect more significant digits than those used for the
declaration of the type.
If you use float, don't expect more than 5 or 6

What's VERY important (especially for people dealing with high reliability
systems ...) is that you can perfectly perform operations on a floating point
type with 15 digits, and nevertheless get only 1 or 2 (or even not any) correct
digit in the result, due to numerical instabilities.

About 10 years ago, I had to rewrote an existing Ada implemention of a method
called CESTAC method, which gave amazing results in giving the number of correct
digits in the result of a floating point operation.
(With surprising results for those who are too much confident in the accuracy
they can expect, and rely only on
type my_float is digits N, with N 'large enough')

I remember this method was originally developped in a French university in Paris
for Ada implementation.
I don't know if this method is still used today.

JF Harrison wrote :

> Want some posts?
> How about some beginner level questions from my end.
>
> One in this post and another following.
>
> First off:
> I got a Very strange error in dividing real numbers.  I divided 12345670.0
> by 100.0 and got 123456.70313 as the answer!  I have certainly able to have
> flawless divisions but also isolated some code that will repeatably
> demonstrate this.  I still dont know what is wrong with the code; perhaps
> you can tell me.
>
> ------------------
> with Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;
> use  Ada.Text_IO, Ada.Integer_Text_IO, Ada.Float_Text_IO;
>
> Procedure PrintReal is
>    Number : Float := 25.0;
>
>    Countup,N,I : Integer;
>    NumberString : String(1..80);
> begin
>
>    Put (12.2, Exp=>0);
>    New_Line;
>    Put (Number, Exp=>0);
>    New_Line;
>    Number := Number + 7.0 * 10.0**(-2);
>    Put (Number, Exp=>0);
>    New_Line;
>
>    N := 10;
>    I := 9;
>    Number := Number + 3.0 * 10.0**(N-I-2);
>    Put (Number, Exp=>0);
>    New_Line;
>
>    Put (Number/10.0, Exp=>0);
>    New_Line;
>
>    -- all the division done above comes out correct
>
>    -- here is the trouble maker
>    Countup := 8;
>    NumberString(1..8) := "12345678";
>    Number := 0.0;  -- reinitialize Number
>           for N in 1 .. Countup loop
>             case NumberString(N) is
>               when '0' => null;
>               when '1' => Number := Number + 1.0 * 10.0**(Countup - N);
>               when '2' => Number := Number + 2.0 * 10.0**(Countup - N);
>               when '3' => Number := Number + 3.0 * 10.0**(Countup - N);
>               when '4' => Number := Number + 4.0 * 10.0**(Countup - N);
>               when '5' => Number := Number + 5.0 * 10.0**(Countup - N);
>               when '6' => Number := Number + 6.0 * 10.0**(Countup - N);
>               when '7' => Number := Number + 7.0 * 10.0**(Countup - N);
>               when '8' => Number := Number + 8.0 * 10.0**(Countup - N);
>               when '9' => Number := Number + 9.0 * 10.0**(Countup - N);
>               when others => Put_Line("non digit in number string");
>             end case;
>    Put (N); Put(" ");
>    Put (Number, Exp=>0);
>    New_Line;
>    -- this answer for Number is correct, so there doesnt seem to be any
> problem with the loop; Number is just a float, nothing special about it
>    -- however, simply dividing Number by 100.0
>    -- provides an incorrect answer
>    -- if (basically) Number mod 100 is does not equal zero (you'll see what
> I mean if you run the program, which is attached - only 756 bytes).
>    Put (N); Put(" ");
>    Put (Number/100.0, Exp=>0);
>    New_Line;
>           end loop;
> end PrintReal;
>
>   ------------------------------------------------------------------------
>                     Name: PrintReal.zip
>    PrintReal.zip    Type: Zip Compressed Data (application/x-zip-compressed)
>                 Encoding: base64





  parent reply	other threads:[~2000-11-15  0:00 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2000-11-14  0:00 Why no new posts ? William J. Thomas
2000-11-14  0:00 ` Vincent Marciante
2000-11-15  7:33   ` Chris Miller
     [not found] ` <002001c04ec8$27db8ee0$b0375140@Fudge>
2000-11-15  0:00   ` David Starner
2000-11-15  0:00   ` Nicolas Brunot [this message]
2000-11-15  0:00     ` Why no new posts ? Float accuracy Jean-Pierre Rosen
2000-11-15  0:00   ` Why no new posts ? Martin Dowie
     [not found] ` <002a01c04eca$dbaecc00$b0375140@Fudge>
2000-11-15  7:24   ` tmoran
2000-11-15  7:38   ` tmoran
2000-11-16  2:44   ` DuckE
replies disabled

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