Re: Where is the correct round() method? Use math.ceil

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

    Re: Where is the correct round() method? Use math.ceil

    Le Monday 28 July 2008 02:35:08 Herman, vous avez écrit :
    Where is the correct round() method?
    Hello,
    >
    I need a round function that _always_ rounds to the higher integer if
    the argument is equidistant between two integers. In Python 3.0, this
    is not the advertised behavior of the built-in function round() as
    >
    seen below:
    >>round(0.5)
    >
    0
    >
    >>round(1.5)
    >
    2
    >
    >>round(2.5)
    >
    2
    Hmm, I don't have the same result in python2.6, I suspect it's a floating
    point precision problem, try just to type "0.5" in the console to see the
    exact representation of this value on your system, it may be just over or
    just down by a small quantity.

    On mine with 2.6 this typically give :
    >>>[26]: 0.5
    ...[26]: 0.5
    >>>[27]: 0.49
    ...[27]: 0.4899999999999 9999
    >>>[29]: 0.51
    ...[29]: 0.5100000000000 0001
    >>>[28]: 1.1
    ...[28]: 1.1000000000000 001
    >>>[35]: round(0.5)
    ...[35]: 1.0


    --
    _____________

    Maric Michaud
Working...