printing anomaly

R

Roy Smith

Paul Rubin said:
What's the deal with this?
print 3.2 3.2
print [3.2] [3.2000000000000002]

Yes, I know that 3.2 isn't an exact binary fraction. I'm wondering
why it's converted differently depending on whether it's in a list.

That's intersting. It looks like it's the difference between str() and
repr():
3.2000000000000002
 
E

Erik Max Francis

Paul said:
What's the deal with this?
print 3.2 3.2
print [3.2] [3.2000000000000002]

Yes, I know that 3.2 isn't an exact binary fraction. I'm wondering
why it's converted differently depending on whether it's in a list.

repr vs. str. The str of the sequence types prints the repr of their
contents.
 
G

Greg Ewing

Paul said:
What's the deal with this?
print 3.2 3.2
print [3.2] [3.2000000000000002]

Yes, I know that 3.2 isn't an exact binary fraction. I'm wondering
why it's converted differently depending on whether it's in a list.

It's not. The difference is that print uses str() to convert
its arguments to strings, whereas lists always use repr()
to convert their elements to strings, regardless of whether
you use str() or repr() on the list as a whole.

A simple experiment shows the difference between str()
and repr() on floats:

Python 2.3.4 (#1, Jun 30 2004, 16:47:37)
[GCC 3.2 20020903 (Red Hat Linux 8.0 3.2-7)] on linux2
Type "help", "copyright", "credits" or "license" for more information.'3.2000000000000002'
 

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,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top