HELP: restore my faith in Python
Bernhard Herzog
herzog at online.de
Mon Mar 6 12:08:18 EST 2000
grant at nowhere. (Grant Edwards) writes:
> If you want to round to the nearest integer, then use
> int(x+0.5). That's been idiomatic in programming for longer
> than I've been around.
Unfortunately, it might produce incorrect results for negative x, at
least on some systems which truncate towards 0 and towards -infinity:
Python 1.5.2 (#1, Nov 13 1999, 12:17:58) [GCC egcs-2.91.66 19990314/Linux (egcs- on linux2
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> x = -0.9
>>> int(x + 0.5)
0
>>> x = +0.9
>>> int(x + 0.5)
1
A better solution is to round() first.
--
Bernhard Herzog | Sketch, a drawing program for Unix
herzog at online.de | http://sketch.sourceforge.net/
More information about the Python-list
mailing list