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.9 required=5.0 tests=BAYES_00 autolearn=ham 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!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: Niklas Holsti Newsgroups: comp.lang.ada Subject: Re: loop problem with exit condition Date: Tue, 26 Oct 2010 10:02:32 +0300 Organization: Tidorum Ltd Message-ID: <8inco8Fe79U1@mid.individual.net> References: <3087f399-2cae-4bc8-bba1-e728943473bb@e14g2000yqe.googlegroups.com> <1spdu1yb2khd0$.rk8xvebgn574.dlg@40tude.net> Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Trace: individual.net miYzkTCsdklf8xDyLPFYzAAcSondPOQlrUwQZ6Jei4k7+t7V34 Cancel-Lock: sha1:zyreI//Lpc9Q+Vv3Y+nsbgbBMak= User-Agent: Mozilla-Thunderbird 2.0.0.24 (X11/20100328) In-Reply-To: Xref: g2news1.google.com comp.lang.ada:14782 Date: 2010-10-26T10:02:32+03:00 List-Id: 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. -- Niklas Holsti Tidorum Ltd niklas holsti tidorum fi . @ .