Re: Floating point numbers

From: William F. Dowling (william.dowling_at_isinet.com)
Date: Wed Mar 07 2001 - 14:56:13 GMT


Dave Hayden writes:
 > "Hoelz, Thomas" <Thomas.Hoelz_at_gedas.de> writes:
 > > 
 > > Hello Bob,
 > > 
 > > in my opinion your experience results from the limited precision a computer
 > > uses to represent floating point numbers.
 > 
 > It's true that floating point numbers often don't exactly represent
 > the intended values, but I don't think that is the problem here.

It is part of the problem.  The example he gave was
   float foo = 1.3;
You would not see this effect (two apparent different values for the
same variable) if you try
   float foo = 1.5;
because 1.5 has an exact representation in binary floating point,
while 1.3 does not.

"1.3" and "1.29999995232" are two ASCII decimal representations of the
same underlying 32-bit binary floating point value, that differ only
in how much they are lying about the precision (32 bits) of the
underlying value. (There are infinitely many such ASCII decimal
representationas -- add as many mor 9's as you like.)

Bob's original question is a good one:

> Can someone explain to me why this should be?  It makes debugging
> code containing floating point numbers a little tricky.

Any use of floating point numbers with a fixed precision
representation is tricky -- not just debugging.  You just have to
assume that you can only rely on 8 or so decimal digits; and there
are lot of traps for the unwary that cause lessening precision if your
code implicitly fails to heed that assumption.  I would think a book
on numerical methods would cover such issues.

<GUESS>
As far as why does UPS print two different things I can only guess
that it has to do with the fact that in one case the value of foo is
read from the text of the program; in the other it is read from a
temporay internal variable.  So the manner of filtering from ASCII
encoded decimal to binary floating point is different.  If it is
inconvenient to remember that "1.3" and "1.29999995232" represent the
same quantity, either read all values from the text of the program, or
all values from intermediate or internal variables.
</GUESS>

I'd be interested to hear a more knowledgable response to my guess
from someone who actually knows :)

Cheers,

Will

-- 
William F. Dowling
ISI/Thomson Scientific (www.isinet.com)
215-386-0100 x-1156


This archive was generated by hypermail 2.1.4 : Wed Feb 13 2002 - 21:51:33 GMT