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.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Thread: a07f3367d7,63043e3a9a3d050f X-Google-Attributes: gida07f3367d7,public,usenet X-Google-NewGroupId: yes X-Google-Language: ENGLISH,UTF8 Path: g2news1.google.com!news4.google.com!proxad.net!feeder1-2.proxad.net!newsfeed.straub-nv.de!noris.net!newsfeed.arcor.de!newsspool4.arcor-online.net!news.arcor.de.POSTED!not-for-mail From: "Dmitry A. Kazakov" Subject: Re: loop problem with exit condition Newsgroups: comp.lang.ada User-Agent: 40tude_Dialog/2.0.15.1 MIME-Version: 1.0 Content-Type: text/plain; charset="utf-8" Content-Transfer-Encoding: 8bit Reply-To: mailbox@dmitry-kazakov.de Organization: cbb software GmbH References: <3087f399-2cae-4bc8-bba1-e728943473bb@e14g2000yqe.googlegroups.com> <1spdu1yb2khd0$.rk8xvebgn574.dlg@40tude.net> <8inco8Fe79U1@mid.individual.net> Date: Tue, 26 Oct 2010 10:24:18 +0200 Message-ID: <17c3nu8khyz9x.vms3qoop7baj$.dlg@40tude.net> NNTP-Posting-Date: 26 Oct 2010 10:24:18 CEST NNTP-Posting-Host: d06487cf.newsspool3.arcor-online.net X-Trace: DXC=g1YiTm8MOCS1`E>oC;JXEZMcF=Q^Z^V3X4Fo<]lROoRQ8kF On Tue, 26 Oct 2010 10:02:32 +0300, Niklas Holsti wrote: > Yannick Duchêne (Hibou57) wrote: >> Le Sun, 26 Sep 2010 11:10:24 +0200, Dmitry A. Kazakov >> a écrit: >>> in your case the simplest way to remove bias would be to multiply the >>> step: >>> […] >>> Inner := Inner_Start + Inner_Step * Long_Float (J - 1); >> >> I did not knew that. Why is an iterative addition less accurate than a >> multiplication ? Does it have something to deal with adjustment of the >> exponent ? (the one of the float's internal representation) > > Yes. If you add two floating-point numbers A+B, and the binary exponent > Ae of A is larger than the exponent Be of B, the mantissa of B will be > shifted right by Ae-Be bits before being added to the mantissa of A, so > the addition loses ("shifts out") about this many of the less > significant bits of the mantissa of B. You are effectively adding A+Bt, > where Bt is B with the lost bits set to zero (assuming positive numbers > -- I haven't figured out what happens with negative numbers). > > If you compute A+N*B by iteratively adding B to A, N times, you are > effectively computing the approximation A+N*Bt (more or less; if the > binary exponent of the growing sum becomes larger than Ae, you will lose > even more precision). The error is N*(B-Bt), thus growing with N. > > In the formula A+N*B, as suggested by Dmitry (and as indeed is good > practice), the multiplication N*B is as accurate as the floating-point > representation allows -- no mantissa bits are lost by exponent > adjustments. If the product is X, the addition A+X is again as accurate > as possible. Some bits of the smaller term (A or X) may again be lost in > the addition, but this loss is not cumulative. For more complicated cases there exist summation algorithms reducing precision loss, e.g. the Kahan algorithm: http://en.wikipedia.org/wiki/Kahan_summation_algorithm (Numeric methods are exiting) -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de