How to print a number as if in the python interpreter?

P

Peng Yu

Hi,

In [2]: sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
Out[2]: 0.9999999999999999

In ipython, I got the above output. But I got a different output from
"print". Is there a way to print exact what I saw in ipython?

~/linux/test/python/man/library/math/fsum$ cat main.py
#!/usr/bin/env python
print sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
~/linux/test/python/man/library/math/fsum$ ./main.py
1.0

Regards,
Peng
 
C

Chris Rebert

Hi,

In [2]: sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
Out[2]: 0.9999999999999999

In ipython, I got the above output. But I got a different output from
"print". Is there a way to print exact what I saw in ipython?

~/linux/test/python/man/library/math/fsum$ cat main.py
#!/usr/bin/env python
print sum([.1, .1, .1, .1, .1, .1, .1, .1, .1, .1])
~/linux/test/python/man/library/math/fsum$ ./main.py
1.0

chris@mbp ~ $ python
Python 2.7.1 (r271:86832, Jul 31 2011, 19:30:53)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.('1.0', '0.9999999999999999')

Beware the subtleties of floating-point arithmetic!
http://docs.python.org/tutorial/floatingpoint.html

Cheers,
Chris
 

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,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top