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

Raymond Hettinger rhettinger at ewtllc.com
Thu Aug 3 05:26:55 CEST 2006


>> Most typical uses of round() don't use the
>> optional argument, true, but I still fail
>> to see what returning an integer instead of
>> a float would buy you.
>
>
> It saves you a function call in the vast
> majority of cases, where an int is what
> you ultimately want.
>

-1 on an extra built-in just to save the time for function call -- this 
is a false optimization, one that is unlikely to ever be the bottleneck 
in any real program's running time.  If it isn't time you care about but 
just hate writing it, what's wrong with a simple helper function like:   
rndint=lambda x:  int(round(x)) ?  There are no shortage of possible 
function pairs that could be proposed (perhaps list(set(x)) or 
int(abs(x)) for example).  Of course, it would be silly to make new 
builtins for them.  IMHO, the case is weak for adding a new variant of 
round().

Also, -10 on changing the semantics of int() to round instead of 
truncate.  The truncating version is found is so many other languages 
and book examples, that it would be a disaster for us to choose a 
different meaning.


Raymond


More information about the Python-Dev mailing list