From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-1.3 required=5.0 tests=BAYES_00,INVALID_MSGID autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,e749bdf6fe4253aa X-Google-Attributes: gid103376,public From: Nicolas Brunot Subject: Re: Why no new posts ? Float accuracy Date: 2000/11/15 Message-ID: <3A125C2A.2E3BD2F@cadwin.com>#1/1 X-Deja-AN: 693832798 Content-Transfer-Encoding: 7bit References: <002001c04ec8$27db8ee0$b0375140@Fudge> X-Accept-Language: fr Content-Type: text/plain; charset=us-ascii X-Complaints-To: abuse@proxad.net X-Trace: nnrp4.proxad.net 974281801 212.27.55.127 (Wed, 15 Nov 2000 10:50:01 CET) Organization: Guest of ProXad - France MIME-Version: 1.0 NNTP-Posting-Date: Wed, 15 Nov 2000 10:50:01 CET Newsgroups: comp.lang.ada Date: 2000-11-15T00:00:00+00:00 List-Id: 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