On Thu, Aug 12, 2010 at 6:14 PM, Ned Deily <n...@acm.org> wrote: > In article <i41p6e$1f...@dough.gmane.org>, > Christian Heimes <li...@cheimes.de> wrote: >>> Is there a way I can keep my floating point number as I typed it? For >>> example, I want 34.52 to be 34.52 and NOT 34.5200000002. >> This isn't a Python issue. Python uses IEEE 754 [1] double precision >> floats like most other languages. 34.52 can't be stored in a float. The >> next valid float is 34.5200000002. > > Well, it is a *bit* of a Python issue since, as others have pointed out, > Python's behavior has changed due to the implementation of Gay's > rounding algorithm in 3.1 and also in 2.7: > > $ python2.6 -c 'print(repr(34.52))' > 34.520000000000003 > $ python2.7 -c 'print(repr(34.52))' > 34.52 > $ python3.1 -c 'print(repr(34.52))' > 34.52 >
But that's not keeping the number the way it was typed. It's just not showing you the exact approximation. It doesn't get rid of rounding errors. >>> 34.52 34.52 >>> >>> _ * 10**10 345200000000.00006 > -- > Ned Deily, > ...@acm.org > > -- > http://mail.python.org/mailman/listinfo/python-list > -- http://mail.python.org/mailman/listinfo/python-list