comp.lang.ada
 help / color / mirror / Atom feed
From: Niklas Holsti <niklas.holsti@tidorum.invalid>
Subject: Re: loop problem with exit condition
Date: Tue, 26 Oct 2010 10:02:32 +0300
Date: 2010-10-26T10:02:32+03:00	[thread overview]
Message-ID: <8inco8Fe79U1@mid.individual.net> (raw)
In-Reply-To: <op.vk5w9gtdule2fv@garhos>

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.

-- 
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
       .      @       .



  reply	other threads:[~2010-10-26  7:02 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 [this message]
2010-10-26  8:24         ` Dmitry A. Kazakov
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