is int(round(val)) safe?
Peter Hansen
peter at engcorp.com
Mon Nov 22 15:58:54 EST 2004
Russell E. Owen wrote:
> I realize this probably a stupid question, but...is it safe to
> round to the nearest integer by using int(round(val))?
Define "safe".
> I suspect it is fine, but wanted to be sure that weird floating point
> representation on some platform might make it unsafe there (i.e. get the
> wrong value due to the floating point value being an approximation) and
> if so, is there a Better Way).
Since round() returns an integer, and since (ignoring really
large integers, since I doubt you're concerned about them
here) floating point values can handle *integer* values
perfectly well, there's no reason it shouldn't do what you
want.
The problem* with floating point is inaccurate representation
of certain _fractional_ values, not integer values.
-Peter
* Well, _one_ of the problems. ;-)
More information about the Python-list
mailing list