Floating point -> string conversions

J

John Fouhy

Can someone explain these differences?
'3.1415926535897931'

Why do the string and default floating point representations get rounded?

(and is this documented anywhere?)
 
J

John Fouhy

I have another related question...
Traceback (most recent call last):
'2147483648'

Is this a bug?

(python 2.3.4)
 
S

Steve Holden

John said:
I have another related question...



Traceback (most recent call last):


'2147483648'

Is this a bug?

(python 2.3.4)
I don't think so. The fact of the matter is that a %d format token
explicitly expects an integral value.

The fact that %s coerces things makes us expect more of other format
tokens, but what would you expect %d to do with 2147483648.5?

regards
Steve
 
J

John Fouhy

Steve Holden said:
The fact that %s coerces things makes us expect more of other format
tokens, but what would you expect %d to do with 2147483648.5?

I'd expect it to give me "2147483648", since that would be consistent
with its other behaviour :)

(eg: eval('"%d" % 1.5') == '1')

I guess what annoys me is that python encourages you to not worry
about the difference between ints and longs (which is great) (unless
you're doing high performance computing), but then something like this
can cause code to stop working just because you've passed a border
which is almost invisible.

(of course, this means that one solution which I would be happy with
is for python to reject any non-integer/long arguments to "%d" :) )
 

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

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top