floating point woes

Discussion in 'Python' started by Hans-Peter Jansen, Feb 15, 2011.

  1. Hi,

    while I usually cope with the woes of floating point issues, this is
    one, that I didn't expect:

    >>> round(2.385, 2)

    2.3799999999999999

    Doesn't the docs say, it's rounded up for this case?

    <quote>
    Values are rounded to the closest multiple of 10 to the power minus n;
    if two multiples are equally close, rounding is done away from 0
    </quote>

    Well, that one is clearly rounding down.

    What's up, eh, down here?

    Pete

    Python 2.6 (r26:66714, Feb 8 2011, 08:50:11)
    [GCC 4.3.2 [gcc-4_3-branch revision 141291]] on linux2
     
    Hans-Peter Jansen, Feb 15, 2011
    #1
    1. Advertising

  2. Hans-Peter Jansen

    Mel Guest

    Hans-Peter Jansen wrote:

    > Hi,
    >
    > while I usually cope with the woes of floating point issues, this is
    > one, that I didn't expect:
    >
    >>>> round(2.385, 2)

    > 2.3799999999999999
    >
    > Doesn't the docs say, it's rounded up for this case?
    >
    > <quote>
    > Values are rounded to the closest multiple of 10 to the power minus n;
    > if two multiples are equally close, rounding is done away from 0
    > </quote>
    >
    > Well, that one is clearly rounding down.
    >
    > What's up, eh, down here?


    2.385 isn't really 2.385:

    Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
    [GCC 4.4.3] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> repr (2.385)

    '2.3849999999999998'
    >>>


    so it correctly rounded down. You need to use Decimal numbers if you want
    numbers that behave the way they look.

    Mel.
     
    Mel, Feb 16, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. H aka N
    Replies:
    15
    Views:
    15,795
    Ben Jones
    Mar 2, 2006
  2. Motaz Saad
    Replies:
    7
    Views:
    6,533
  3. Replies:
    4
    Views:
    1,326
    Default User
    Feb 22, 2006
  4. Saraswati lakki
    Replies:
    0
    Views:
    1,390
    Saraswati lakki
    Jan 6, 2012
  5. teeshift
    Replies:
    2
    Views:
    285
    Chris Pearl
    Dec 1, 2006
Loading...

Share This Page