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=-0.1 required=5.0 tests=AXB_XMAILER_MIMEOLE_OL_024C2, BAYES_00,MAILING_LIST_MULTI,REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,2d09fa6592c24a21 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2002-03-10 04:59:03 PST Path: archiver1.google.com!news1.google.com!newsfeed.stanford.edu!newsfeeds.belnet.be!news.belnet.be!deine.net!freenix!enst!enst.fr!not-for-mail From: "David C. Hoos, Sr." Newsgroups: comp.lang.ada Subject: Re: I think - it's a bug... Date: Sun, 10 Mar 2002 06:58:37 -0600 Organization: ENST, France Sender: comp.lang.ada-admin@ada.eu.org Message-ID: References: <3C8B184D.49214059@yahoo.com> Reply-To: comp.lang.ada@ada.eu.org NNTP-Posting-Host: marvin.enst.fr Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: avanie.enst.fr 1015765142 43374 137.194.161.2 (10 Mar 2002 12:59:02 GMT) X-Complaints-To: usenet@enst.fr NNTP-Posting-Date: Sun, 10 Mar 2002 12:59:02 +0000 (UTC) Return-Path: X-Priority: 3 X-MSMail-Priority: Normal X-Mailer: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org X-Mailman-Version: 2.0.8 Precedence: bulk List-Help: List-Post: List-Subscribe: , List-Id: comp.lang.ada mail<->news gateway List-Unsubscribe: , Errors-To: comp.lang.ada-admin@ada.eu.org X-BeenThere: comp.lang.ada@ada.eu.org Xref: archiver1.google.com comp.lang.ada:21016 Date: 2002-03-10T06:58:37-06:00 This is not a bug. The problem comes about by not being aware of the nature of floating point representation in computers. In your example, the value of dz (0.3) is not exactly representable in binary -- it is an infinitely repeating binary fraction. Hence, all values of z that you compute are not exactly representable. Thus, if the value of dz was rounded upward to the next lsb when converting from decimal to binary, all values of z will be slightly larger than their true value. The only solution is to test the values of which you are about to take the square root, and if the absolute value is less than some very small threshold, set the value to 0.0. Alternatively, if you are sure (given the nature of the problem you are solving) that z can never be greater than b, you could replace (z/b) by Float'Min (1.0, z/b). Additionally, you might want to consider the following statement from the GNAT User's Guide: In particular, if the Ada code will do any floating-point operations, then the FPU must be setup in an appropriate manner. For the case of the x86, for example, full precision mode is required. The procedure GNAT.Float_Control.Reset may be used to ensure that the FPU is in the right state. Finally you can obtain greater precision by using Long_Float or Long_Long_Float instead of Float. ----- Original Message ----- From: "Anatoly Chernyshev" Newsgroups: comp.lang.ada To: Sent: March 10, 2002 2:24 AM Subject: I think - it's a bug... > Hello everybody! > > Look at this piece of code: > > ---------------------------------------------------------------------------- --------------------- > > WITH ada.text_io,ada.numerics.elementary_functions; > USE ada.text_io, ada.numerics.elementary_functions; > PROCEDURE el_stat_fun IS > b : float := 7.5; > a:float:=4.0; > rr, z : float; > r : float := 4.0; > dz : float := 0.3; > BEGIN > FOR k IN 0..integer(b/dz) LOOP > z:=float(k)*dz; > put_line (float'image(1.0-(z/b))); > rr:=-a*sqrt((1.0-(z/b)**2))+a+r; > END LOOP; > END el_stat_fun; > ---------------------------------------------------------------------------- ------------------------ > > > > > When compiled using GNAT 3.14 (WinNT sp 6) it raises > ADA.NUMERICS.ARGUMENT_ERROR in sqrt function when k goes to 25 because > the argument for sqrt becomes > negative (like -X.XXXXXE-07 ... And this is a headache No 1). > However, if one comments out the put_line string - everything works > fine. > > I don't know whether this is a bug or feature, but any kind of help for > how to avoid the situation when 1.0-1.0/1.0 yields something different > from 0.0 will be greatly appreciated. > > Thanks in advance, > > Anatoly. > > > > > > > _______________________________________________ > comp.lang.ada mailing list > comp.lang.ada@ada.eu.org > http://ada.eu.org/mailman/listinfo/comp.lang.ada >