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

Maric Michaud maric at aristote.info
Mon Jul 28 14:30:34 EDT 2008


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.48999999999999999

>>>[29]: 0.51
...[29]: 0.51000000000000001

>>>[28]: 1.1
...[28]: 1.1000000000000001

>>>[35]: round(0.5)
...[35]: 1.0


-- 
_____________

Maric Michaud



More information about the Python-list mailing list