Representation of floats (-> Mark Dickinson?)

jmfauth wxjmfauth at gmail.com
Tue Sep 6 09:37:51 EDT 2011


This is just an attempt to put the
http://groups.google.com/group/comp.lang.python/browse_thread/thread/a008af1ac2968833#
discussion at a correct level.

With Python 2.7 a new float number representation (the David Gay's
algorithm)
has been introduced. If this is well honored in Python 2.7, it
seems to me, there are some missmatches in the Py3 series.

>>> sys.version
'2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)]'
>>> 0.1
0.10000000000000001
>>> print 0.1
0.1
>>> 1.1 * 1.1
1.2100000000000002
>>> print 1.1 * 1.1
1.21
>>> print repr(1.1 * 1.1)
1.2100000000000002
>>>


>>> sys.version
2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
>>>
>>> 0.1
0.1
>>> print 0.1
0.1
>>> 1.1 * 1.1
1.21
>>> print 1.1 * 1.1
1.21
>>> print repr(1.1 * 1.1)
1.2100000000000002
>>>


>>> sys.version
'3.1.4 (default, Jun 12 2011, 15:05:44) [MSC v.1500 32 bit (Intel)]'
>>> 0.1
0.1
>>> print(0.1)
0.1
>>> 1.1 * 1.1
1.2100000000000002
>>> print(1.1 * 1.1)
1.21
>>> print(repr(1.1 * 1.1))
1.2100000000000002
>>> '{:g}'.format(1.1 * 1.1)
'1.21'



>>> sys.version
'3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)]'
>>> 0.1
0.1
>>> print(0.1)
0.1
>>> 1.1 * 1.1
1.2100000000000002
>>> print (1.1 * 1.1)
1.2100000000000002
>>> print(repr((1.1 * 1.1)))
1.2100000000000002
>>>
>>> '{:g}'.format(1.1 * 1.1)
'1.21'
>>>

jmf



More information about the Python-list mailing list