Representation of floats (-> Mark Dickinson?)

J

jmfauth

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.
'2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)]'
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
 
C

casevh

This is just an attempt to put thehttp://groups.google.com/group/comp.lang.python/browse_thread/thread/...
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.

'2.5.4 (r254:67916, Dec 23 2008, 15:10:54) [MSC v.1310 32 bit
(Intel)]'
1.2100000000000002

2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]


1.2100000000000002

I tried this with the same version of Python and I get:
sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
1.1 * 1.1 1.2100000000000002
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

'3.2.2 (default, Sep  4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)]'
1.2100000000000002
'1.21'

I get same results as you do for Python 3.1.4 and 3.2.2. IIRC, Python
3.2 changed (for floats) __str__ to call __repr__. That should explain
the difference between 3.1.4 and 3.2.2

Also note that 1.1 * 1.1 is not the same as 1.21.
(1362338887279575, 1125899906842624)

This doesn't explain why 2.7.2 displayed a different result on your
computer. What do you get for as_integer_ratio() for (1.1 * 1.1) and
(1.21)?

casevh
 
J

jmfauth

Also note that 1.1 * 1.1 is not the same as 1.21.


(5449355549118301, 4503599627370496)>>> (1.21).as_integer_ratio()

(1362338887279575, 1125899906842624)

This doesn't explain why 2.7.2 displayed a different result on your
computer. What do you get for as_integer_ratio() for (1.1 * 1.1) and
(1.21)?


Sure. I just picked up these numbers/expressions by chance. They
came to my mind following the previous discussion.

Sticking with the latest versions:

sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'
(1.1 * 1.1).as_integer_ratio() (5449355549118301L, 4503599627370496L)
(1.21).as_integer_ratio() (1362338887279575L, 1125899906842624L)

sys.version '3.2.2 (default, Sep 4 2011, 09:51:08) [MSC v.1500 32 bit (Intel)]'
(1.1 * 1.1).as_integer_ratio() (5449355549118301, 4503599627370496)
(1.21).as_integer_ratio() (1362338887279575, 1125899906842624)


Has "long" not disappeared 2.7?
I have not the skill to dive into the machinery. I have
only some theroretical understanding and I'm a little bit
confused and have to face "there something strange
somewhere".

Test on Windows 7, 32 bits.


jmf
 
M

Mark Dickinson

IIRC, Python
3.2 changed (for floats) __str__ to call __repr__.

Yes, exactly: str and repr of a float are identical in Python 3.2 +

I'm also puzzled by the

2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
[...]1.21

in jmf's message. Cut-and-paste typo?
 
J

jmfauth

IIRC, Python
3.2 changed (for floats) __str__ to call __repr__.

Yes, exactly:  str and repr of a float are identical in Python 3.2 +

I'm also puzzled by the

2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]
[...]>>> 1.1 * 1.1

1.21

in jmf's message.  Cut-and-paste typo?


No. But, it's *my* mistake. I'm using a modified sys.displayhook which
uses a print statement (mainly for language reason). If forgot to
reset to the initial/default state for these tests when working
with too many opened interactive interpreters.
Sorry for the noise.

sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

1.1 * 1.1 1.21
'éléphant' éléphant

sys.displayhook = sys.__displayhook__
1.1 * 1.1 1.2100000000000002
'éléphant' '\xe9l\xe9phant'
sys.version '2.7.2 (default, Jun 12 2011, 15:08:59) [MSC v.1500 32 bit (Intel)]'

jmf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top