round function error???

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

    round function error???

    Isn't this a mistake???
    >>round(3499.34 9439034,44)
    3499.3494390340 002
    >>round(_,2)
    3499.3499999999 999
    >>round(_,1)
    3499.4000000000 001

    My Python 2.5.1 spat that out..
  • Mark Dickinson

    #2
    Re: round function error???

    On Jul 18, 10:17 pm, Anthony <ajdam...@gmail .comwrote:
    Isn't this a mistake???
    Which 'this'? That is, what were you expecting?

    If you're objecting to the fact that the second result
    produces 3499.3499999999 999 instead of 3499.35, then
    no, that's not a mistake; see

    Contents: General Python FAQ- General Information- What is Python?, What is the Python Software Foundation?, Are there copyright restrictions on the use of Python?, Why was Python created in the fi...


    for more information. But I'm guessing that you're
    questioning the fact that a value that's apparently
    *less* than 3499.35 is rounded up to 3499.4, rather
    than down to 3499.3. ?

    Then yes, I'd agree that's less than ideal, though I
    don't consider it a particularly serious bug.
    It's been on my list of things to fix for a while.
    (See http://bugs.python.org/issue1869 ).
    Contributions welcome!

    Mark

    Comment

    • Terry Reedy

      #3
      Re: round function error???



      Anthony wrote:
      Isn't this a mistake???
      >
      >>>round(3499.3 49439034,44)
      3499.3494390340 002
      >>>round(_,2)
      3499.3499999999 999
      >>>round(_,1)
      3499.4000000000 001
      >
      My Python 2.5.1 spat that out..
      No, round() return binary floats that, in general, cannot represent
      decimal floats exactly. Formatted printing gives what you expect.
      >>'%8.2f' % x
      ' 3499.35'

      Comment

      • Mark Dickinson

        #4
        Re: round function error???

        On Jul 18, 11:15 pm, Terry Reedy <tjre...@udel.e duwrote:
        No, round() return binary floats that, in general, cannot represent
        decimal floats exactly.  Formatted printing gives what you expect.
         >>'%8.2f' % x
        ' 3499.35'
        Sure. But it's still true that the second printed value
        (printed as 3499.3499999999 999) is strictly less than 3499.35,
        so when rounding to 1 decimal place an ideal rounding routine
        would round it *down* to 3499.3 instead of up to 3499.4. This
        is what '%.1f' does, for example, on a platform where printf
        does correct rounding:

        Python 2.5.1 (r251:54863, Jan 17 2008, 19:35:17)
        [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
        Type "help", "copyright" , "credits" or "license" for more information.
        >>x = 3499.35
        >>x
        3499.3499999999 999
        >>print '%.1f' % x
        3499.3
        >>print round(x, 1)
        3499.4

        Mark

        Comment

        • John Machin

          #5
          Re: round function error???

          On Jul 19, 8:05 am, Mark Dickinson <dicki...@gmail .comwrote:
          On Jul 18, 10:17 pm, Anthony <ajdam...@gmail .comwrote:
          >
          Isn't this a mistake???
          >
          Which 'this'? That is, what were you expecting?
          >
          If you're objecting to the fact that the second result
          produces 3499.3499999999 999 instead of 3499.35, then
          no, that's not a mistake; see
          >
          http://www.python.org/doc/faq/genera...-point-calcula...
          >
          for more information. But I'm guessing that you're
          questioning the fact that a value that's apparently
          *less* than 3499.35 is rounded up to 3499.4, rather
          than down to 3499.3. ?
          "apparently " being the operative word.
          >>x = 3499.35
          >>repr(x)
          '3499.349999999 9999'
          >>float(repr(x) ) == x
          True
          >>float(repr(x) ) < x
          False
          >
          Then yes, I'd agree that's less than ideal, though I
          don't consider it a particularly serious bug.
          It's been on my list of things to fix for a while.
          I'd suggest adding this to the list of floating point strangenesses in
          the FAQ and/or the appendix to the tutorial, rather than "fixing" it.
          The example you give there seems to be somewhat more deserving of a
          fix.

          Cheers,
          John

          Comment

          • Mark Dickinson

            #6
            Re: round function error???

            On Jul 19, 12:20 am, John Machin <sjmac...@lexic on.netwrote:
            On Jul 19, 8:05 am, Mark Dickinson <dicki...@gmail .comwrote:
            for more information.  But I'm guessing that you're
            questioning the fact that a value that's apparently
            *less* than 3499.35 is rounded up to 3499.4, rather
            than down to 3499.3.  ?
            >
            "apparently " being the operative word.
            Well, it's not just an apparent problem: the closest
            floating-point number to 3499.35 really *is* less than
            3499.35. A nice way to check this is using the new
            fractions module in 2.6, which allows exact conversions
            of floats and Decimals into rational numbers:

            Python 2.6b2+ (trunk:65155, Jul 20 2008, 15:39:46)
            [GCC 4.0.1 (Apple Inc. build 5484)] on darwin
            Type "help", "copyright" , "credits" or "license" for more information.
            >>from decimal import Decimal
            >>from fractions import Fraction
            >>x = 3499.35
            >>y = Decimal('3499.3 5')
            >>Fraction.from _float(x)
            Fraction(769515 2029315891, 2199023255552)
            >>Fraction.from _decimal(y)
            Fraction(69987, 20)
            [54933 refs]
            >>Fraction.from _float(x) < Fraction.from_d ecimal(y)
            True

            Mark

            Comment

            • John Machin

              #7
              Re: round function error???

              On Jul 21, 12:56 am, Mark Dickinson <dicki...@gmail .comwrote:
              On Jul 19, 12:20 am, John Machin <sjmac...@lexic on.netwrote:
              >
              On Jul 19, 8:05 am, Mark Dickinson <dicki...@gmail .comwrote:
              for more information. But I'm guessing that you're
              questioning the fact that a value that's apparently
              *less* than 3499.35 is rounded up to 3499.4, rather
              than down to 3499.3. ?
              >
              "apparently " being the operative word.
              >
              Well, it's not just an apparent problem: the closest
              floating-point number to 3499.35 really *is* less than
              3499.35.
              I'm well aware of that. My point is that I hope that you weren't
              planning on changing that behaviour in an unannounced unstaged
              manner.

              Comment

              Working...