Web lists-archives.com

Re: [Mingw-users] msvcrt printf bug

Peter Rockett wrote:
> Guys
> At the risk of wading into a thread that has become very heated,
> could I drag this back to technical matters.

With similar trepidation...

> I think everybody (apart maybe from the OP) agrees how floating point
> numbers behave. Keith makes a good point about rounding. Can I toss
> in another feature that changing the compiler optimisation level
> often reorders instructions meaning that rounding errors accumulate
> in different ways. So changing the optimisation level often slightly
> changes the numerical answers. :-\

Another aspect which I think has been mentioned in passing a couple of 
times, but not expanded on, is that how the processor's floating-point 
registers are utilised can also have an effect. These registers are 
typically higher precision than the in-memory representation of floats, 
so can store intermediate results more accurately, but there are a 
limited number of them - so sometimes some intermediate results have to 
be written out to memory (at reduced precision) and read back later.

An example, working in decimal for readability... Let's say my in-memory 
"floating-point decimal" can represent 2 significant figures (SF). My 
processor has a register which can hold 3SF. I want to calculate the sum 
of four values, which happen to be 0.64, 0.65, 0.66, 0.67.

Storing each intermediate result in the 3SF register, I'd calculate:
   + 0.65
   = 1.29
   + 0.66
   = 1.95
   + 0.67
   = 2.62
The final result stored back to memory (rounded to 2SF) is 2.6.

If each intermediate result has to be written out to memory, where only 
2SF can be represented, I'd calculate:
   + 0.65
   = 1.3  (1.29 rounded to 2SF)
   + 0.66
   = 2.0  (1.96 rounded to 2SF)
   + 0.67
   = 2.7  (2.67 rounded to 2SF)
The final result is 2.7, which is different from the result obtained by 
storing the intermediate values in the register.

For a simple example like this, of course the register would almost 
certainly be used. With several floating-point registers available, even 
relatively complex calculations could be performed without writing 
intermediate values out to memory. However, there comes a point where 
there may not be enough registers available to perform a very complex 
calculation without writing some intermediate values out to memory and 
reading them back when needed later. In that case, different compilers 
(or even the same compiler with different optimisations) might allocate 
the registers differently, affecting the exact value of the final 
result. A different processor might not even have the same number or 
size of registers available.

In this case, it's not just that some text formatting code produces 
different output for the same floating-point number - the result might 
not even be exactly the same floating-point number depending on how the 
registers are used.


Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
MinGW-users mailing list

This list observes the Etiquette found at 
We ask that you be polite and do the same.  Disregard for the list etiquette may cause your account to be moderated.

You may change your MinGW Account Options or unsubscribe at:
Also: mailto:mingw-users-request@xxxxxxxxxxxxxxxxxxxxx?subject=unsubscribe