working of round()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Krishna.K.1900@gmail.com

    working of round()

    Does round() always perfectly return the output expected or are there
    some artifacts which don't allow perfect functionality

    Using python 2.5:
    >>round(12.23 4, 2)
    12.23
    >>round(12.23 4, 3)
    12.234
    >>round(12.23 4, 1)
    12.199999999999 999
    >>>
    but was expecting 12.2

    Also, for round(x,n), can't 'x' be an expression

    round(5.25/2, 2)

    was expecting 2.62 , but
    >>round(5.25/2, 2)
    2.6299999999999 999

  • Robert Kern

    #2
    Re: working of round()

    Krishna.K.1900@ gmail.com wrote:
    Does round() always perfectly return the output expected or are there
    some artifacts which don't allow perfect functionality
    >
    Using python 2.5:
    >>>round(12.234 , 2)
    12.23
    >>>round(12.234 , 3)
    12.234
    >>>round(12.234 , 1)
    12.199999999999 999
    >
    but was expecting 12.2

    Also, for round(x,n), can't 'x' be an expression
    >
    round(5.25/2, 2)
    >
    was expecting 2.62 , but
    >
    >>>round(5.25/2, 2)
    2.6299999999999 999
    round(x, n) essentially does the following:

    math.floor(x * 10**n + 0.5) / 10**n

    Since (5.25/2)*100 == 262.5, adding 0.5 gives 263.0 and ultimately 2.63 as the
    rounded number. round() does not do anything more complicated like round-to-even.

    Use the decimal module if you want decimal arithmetic rather than binary
    floating point.

    --
    Robert Kern

    "I have come to believe that the whole world is an enigma, a harmless enigma
    that is made terrible by our own mad attempt to interpret it as though it had
    an underlying truth."
    -- Umberto Eco

    Comment

    • Steven Bethard

      #3
      Re: working of round()

      Krishna.K.1900@ gmail.com wrote:
      Does round() always perfectly return the output expected or are there
      some artifacts which don't allow perfect functionality
      >
      Using python 2.5:
      >>>round(12.234 , 2)
      12.23
      >>>round(12.234 , 3)
      12.234
      >>>round(12.234 , 1)
      12.199999999999 999
      >
      but was expecting 12.2
      >
      Also, for round(x,n), can't 'x' be an expression
      >
      round(5.25/2, 2)
      >
      was expecting 2.62 , but
      >
      >>>round(5.25/2, 2)
      2.6299999999999 999
      You're running into floating-point issues (e.g. it's impossible to
      represent 2.63 perfectly in binary). What are you really trying to do?
      If you just want to format these with only two decimal places, use
      string formatting::
      >>'%.2f' % 12.234
      '12.23'
      >>'%.2f' % (5.25 / 2)
      '2.63'

      I'm not sure why you would have expected 2.62 for the latter when::
      >>5.25 / 2
      2.625

      STeVe

      Comment

      • subscriber123

        #4
        Re: working of round()

        On Apr 15, 8:06 pm, Krishna.K.1...@ gmail.com wrote:
        Does round() always perfectly return the output expected or are there
        some artifacts which don't allow perfect functionality
        >
        Using python 2.5:
        >
        >round(12.234 , 2)
        12.23
        >round(12.234 , 3)
        12.234
        >round(12.234 , 1)
        12.199999999999 999
        >
        but was expecting 12.2
        >
        Also, for round(x,n), can't 'x' be an expression
        >
        round(5.25/2, 2)
        >
        was expecting 2.62 , but
        >
        >round(5.25/2, 2)
        >
        2.6299999999999 999
        The problem is that floats are encoded as fractions where the
        denominator is an exponent of 2.
        2.63 is not representable as such a fraction.
        2.6299999999999 999999999999999 99999999... is the closest fraction.
        Rounding this number will only give you the same thing.
        If you want decimals to act as expected, use the Decimal class in
        module decimal. It works as expected, but is much slower.

        Comment

        Working...