repr of floating points

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Noam Raphael

    repr of floating points

    Hello,

    repr(0.1) is '0.100000000000 00001', 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
  • Josiah Carlson

    #2
    Re: repr of floating points

    > repr(0.1) is '0.100000000000 00001', in my Python 2.3.[color=blue]
    > 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"?[/color]

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

    - Josiah

    Comment

    Working...