[Python-Dev] Rounding float to int directly (Re: struct module and coercing floats to integers)

Greg Ewing greg.ewing at canterbury.ac.nz
Tue Aug 1 02:30:09 CEST 2006


Raymond Hettinger wrote:

> I think this would harm more than it would help.  It more confusing to 
> have several rounding-thingies to choose from than it is have an 
> explicit two-step.

But is it more confusing enough to be worth forcing
everyone to pay two function calls instead of one
in the most common case?

If I'm right that rounding-to-int is much more
commonly needed than rounding-to-float, the least
confusing thing would be for the builtin round()
to return an int, and have something somewhere
else, such as math.fround(), for round-to-float.

> BTW,  I thought the traditional idiom (for positive numbers) was:  
> int(x+.5)

It's the "for positive numbers" that's the problem
there. Most of my uses for round() involve graphics
coordinates, which I can't be sure won't be negative.
It's not immediately obvious what this will do with
negative numbers. It's not even immediately obvious
that it's doing round-to-nearest-integer unless
you're familiar with the idiom. A single well-named
function would be much more explicit.

--
Greg


More information about the Python-Dev mailing list