[triangle-zpug] rounding errors with floating point operations

Joseph Mack NA3T jmack at wm7d.net
Sat Jul 19 14:21:45 UTC 2008

This is about floating point ops, and not specific to 
python. It just came up when I was writing python code.

If you do the following floating point ops

y=x/n	#n some floating point number

I would expect due to the finite precision of floating point 
representation, that some digits would fall off the end when 
doing y=x/n and that the z you get would give z!=x. However 
I find that z==x. Here's an example where n is a power of 2.

>>> n_128=2**128
>>> print n_128
>>> x=0.1
>>> y=x/n_128
>>> z=y*n_128
>>> print "%30.30g, %30.30g, %30.30g" %(x,y,z)
0.100000000000000005551115123126, 2.93873587705571893305445304302e-40, 0.100000000000000005551115123126

I'm happy with this one. Dividing by a power of 2 
just changes the exponent and not the mantissa in the 
floating point prepresentation of a number. So I wouldn't 
expect the precision of the mantissa to change.

Here's an example when the divisor should affect the 
mantisssa, but still I don't see any sign of digits falling 
off the right side of the mantissa due to the division

>>> n_prime=149.0
>>> y=x/n_prime
>>> z=y*n_prime
>>> print "%30.30g, %30.30g, %30.30g" %(x,y,z)
0.100000000000000005551115123126, 0.000671140939597315508424735241988, 0.100000000000000005551115123126

Why doesn't division of reals loose precision?

Thanks Joe
Joseph Mack NA3T EME(B,D), FM05lw North Carolina
jmack (at) wm7d (dot) net - azimuthal equidistant map
generator at http://www.wm7d.net/azproj.shtml
Homepage http://www.austintek.com/ It's GNU/Linux!

More information about the triangle-zpug mailing list