[Tutor] decimal module and precision
Eike Welk
eike.welk at gmx.net
Mon Jan 31 14:24:42 CET 2011
Hello Richard!
In your code:
d(123.2345274523452345235432452345)
you are entering the number as a `float` and you are loosing the precision
already when the code is compiled. To retain the precision all constants must
be entered as strings.
For illustration see this part of an interactive Python session:
>>> 123.123456789012345678901
123.12345678901235
The other issue with floating point is, that only very few numbers can be
accurately represented. These numbers must be composed from (including
negative) powers of two. Like this: 0.5 + 0.25 + 0.125 + 0.0625 ...
Numbers that cant be represented accurately:
>>> 0.1
0.10000000000000001
>>> 0.3
0.29999999999999999
It would probably be a good idea, if the Python compiler would issue a warning
when it encounters a `float` constant with excessive precision.
Eike.
More information about the Tutor
mailing list