[Python-Dev] Floating point -> string conversions

Batista, Facundo FBatista at uniFON.com.ar
Fri Nov 12 13:48:23 CET 2004


There's a thread in the standard list (with this very Subject) that talks
about implicit coercing with "%d" in strings replacement.

jfouhy posted this example:

>>> pow(2, 31)
2147483648L
>>> '%d' % 2147483647.0              # python will convert to int
'2147483647'
>>> '%d' % 2147483648.0              # too big for an int, so error.
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: int argument required
>>> '%d' % long(2147483648.0)        # but yet, no trouble accepting a long.
'2147483648'
>>> '%d' % int(2147483648.0)         # and int() converts to long anyway
'2147483648'


I think that here's a bug, but don't know where:

Should %d accept only integer values? So this is a bug:

>>> '%d' % 2147483647.0
'2147483647'

Should %d accept also floats, and make the conversion? So there're two bugs:

- In the doc (it's not explicited that it should accept a other data types)

- In this behaviour difference:

>>> '%d' % 2147483647.0
'2147483647'
>>> '%d' % 2147483648.0
Traceback (most recent call last):
...
TypeError: int argument required


I really don't know about opening a bug, because I don't know which to open,
:p

.	Facundo
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/python-dev/attachments/20041112/929d3980/attachment.html


More information about the Python-Dev mailing list