newbie floating point question
phr-n2002a at nightsong.com
Thu Feb 21 20:59:04 CET 2002
Alex <alexhelder at R191.net> writes:
> Why does this happen ?
> >>> .2
> Is this the correct behavior for the Python 2.2 interpreter ? (version info
> is printed below )
It's because 0.2 (= 1/5) can't be represented as an exact binary
number. Only numbers like 0.25 (= 1/4), 0.125 (1/8), 0.375 (1/4 +
1/8), and so forth can be represented exactly. 0.2 gets converted
into an inexact approximation that differs in the 17th or so decimal
place. When you print it out displaying all the digits, you can see
the discrepancy. It's normal and expected behavior. If you're doing
serious numerical computing you have to worry about this kind of thing.
For more casual purposes you probably can ignore the tiny error.
More information about the Python-list