comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: loop problem with exit condition
Date: Tue, 26 Oct 2010 10:24:18 +0200
Date: 2010-10-26T10:24:18+02:00	[thread overview]
Message-ID: <17c3nu8khyz9x.vms3qoop7baj$.dlg@40tude.net> (raw)
In-Reply-To: 8inco8Fe79U1@mid.individual.net

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 
>> <mailbox@dmitry-kazakov.de> 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



  reply	other threads:[~2010-10-26  8:24 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2010-09-26  8:14 loop problem with exit condition Ada novice
2010-09-26  8:16 ` Ada novice
2010-09-26  9:10   ` Dmitry A. Kazakov
2010-09-26 10:19     ` Ada novice
2010-10-26  2:59     ` Yannick Duchêne (Hibou57)
2010-10-26  7:02       ` Niklas Holsti
2010-10-26  8:24         ` Dmitry A. Kazakov [this message]
2010-10-26 12:38       ` Brian Drummond
replies disabled

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