repr of floating points

N

Noam Raphael

Hello,

repr(0.1) is '0.10000000000000001', in my Python 2.3.
From reading the article "Floating Point Arithmetic: Issues and
Limitations", I understand that it's the closest 17-digit decimal
representation of the binary floating-point number actually stored.

However, I don't understand why this should be what repr of floats
returns. repr should, if possible, return a string that evaluating it
will be == to the original object. But eval("0.1") obviously returns the
same floating point as 0.1 does! So why not make repr of floats return
the *shortest* decimal representation that will evaluate to the stored
binary number? Specifically, why not make repr(0.1) be "0.1"?

Thanks,
Noam Raphael
 
J

Josiah Carlson

repr(0.1) is '0.10000000000000001', in my Python 2.3.
From reading the article "Floating Point Arithmetic: Issues and
Limitations", I understand that it's the closest 17-digit decimal
representation of the binary floating-point number actually stored.

However, I don't understand why this should be what repr of floats
returns. repr should, if possible, return a string that evaluating it
will be == to the original object. But eval("0.1") obviously returns the
same floating point as 0.1 does! So why not make repr of floats return
the *shortest* decimal representation that will evaluate to the stored
binary number? Specifically, why not make repr(0.1) be "0.1"?

Search the archives of python-dev. The answer lies within. Search
before asking next time.

- Josiah
 

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,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top