Why the nonsense number appears?

Yu-Xi Lim yuxi at ece.gatech.edu
Mon Oct 31 04:36:37 EST 2005


Johnny Lee wrote:
<snip>
>    Why are there so many nonsense tails? thanks for your help.

I guess you were expecting 0.039? You first need to understand floating 
point numbers:

http://docs.python.org/tut/node16.html

What you see are the effects of representation errors.

The solution is presented here:
http://www.python.org/peps/pep-0327.html

But briefly, it means your code should read:

from decimal import *
t1 = "1130748744"
t2 = "461"
t3 = "1130748744"
t4 = "500"
time1 = t1+"."+t2
time2 = t3+"."+t4
print time1, time2
Decimal(time2) - Decimal(time1)



More information about the Python-list mailing list