is int(round(val)) safe?

Tim Peters tim.peters at gmail.com
Tue Nov 23 04:14:57 CET 2004

```[Peter Hansen]
...
> I wonder if it would be appropriate to say something along
> the lines of '''round() returns an integer, but not an "int".'''

Well, round() is a 2-argument function, whose second argument defaults
to 0.  It's quite defensible to say that round returns an integer
value when the second argument is 0.

> Obviously the mathematicians will have something to say about
> this.  In computers, 1.0 may not be an integer data type, but
> I think in math it's still considered an integer.

Depends on which mathematician you're talking to.  The integer 1 is
most often defined as the set containing the empty set, or, in a
suitably restricted set theory, with the set of all sets containing 1
element (which is a proper class in most set theories).  A real, OTOH,
is a godawful construction that Americans typically don't learn until
after they've completed "calculus" and gone on to "analysis".

<wink>.  Yes, 1.0 is an integer!  In fact, so is 1.9998e143:  all
sufficient large floats are exact integers.  That's why, e.g.,
math.ceil() and math.floor() return arguments like 1.9998e143
unchanged -- such inputs are already integers.

> I am most definitely not going to claim authority in this area, however,
> since as an engineer I consider 1.0 and 1 merely "equal to a
> first approximation". <wink>

If they differ at all, then e = 1.0 - 1 must be nonzero.  Since it
isn't, they're identical <wink>.

```