[triangle-zpug] rounding errors with floating point operations

Chris Calloway cbc at unc.edu
Mon Jul 21 19:23:55 UTC 2008


On Jul 19, 2008, at 10:21 AM, Joseph Mack NA3T wrote:
> 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
>
> x=0.1
> y=x/n	#n some floating point number
> z=y*n
>
> 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
> 340282366920938463463374607431768211456
>>>> 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!
>
> _______________________________________________
> triangle-zpug mailing list
> triangle-zpug at starship.python.net
> http://starship.python.net/mailman/listinfo/triangle-zpug


It's all in all the print spec. It's not giving you an accurate  
representation of the precision when you specify more precision in the  
print spec than there is in a C double. It's all still  
0.10000000000000001.

-- 
Sincerely,

Chris Calloway
http://www.secoora.org
office: 332 Chapman Hall   phone: (919) 599-3530
mail: Campus Box #3300, UNC-CH, Chapel Hill, NC 27599






More information about the triangle-zpug mailing list