[Python-Dev] Rounding float to int directly ...

Nick Coghlan ncoghlan at gmail.com
Wed Aug 2 14:02:41 CEST 2006


Michael Chermside wrote:
> Greg Ewing writes:
>> [I propose for Py3k that] builtin round()
>> to return an int, and have something somewhere
>> else, such as math.fround(), for round-to-float.
> 
> +1.
> 
> The need to convert float to int is pervasive. The need to round (binary)
> floats to integer (or other decimal) floats is _much_ less common.
> Furthermore, the need for "round to nearest int" is much more common
> than the need for "integer floor" (although as pointed out, each could
> be defined in terms of the other except for certain rules for rounding
> exact halves). And most telling of all, if I were to stop 10 random
> programmers on the street, most of them not familiar with Python, and
> ask them to guess what the function named "round" did, I'm guessing
> the majority would think of float-to-int. It's a nice feature of Python
> that "it just works".

The potential issue with accidentally triggering truncating division also goes 
away in Py3k, so that's another point in favour of switching round() to return 
an integer result.

Another option would be to provide this as a method of float objects (similar 
to decimal).


> Nick Maclaren write:
>> Decimal doesn't even help people who care about accuracy.
> 
> Not true! The float class is incapable of maintaining 700 digits of
> precision, but Decimal handles it just fine. (Why you would WANT
> more than around 53 bits of precision is a different question, but
> Decimal handles it.)

The decimal module's default settings actually provide 93 bits or so of 
precision (28 decimal digits).

Sure, there may still be cases where you have to care about dynamic range, but 
if two numbers are separated by 28 orders of magnitude the smaller one really 
is pretty damn close to zero as far as the second one is concerned (in a fixed 
point system, the second one usually *would* be zero).

Cheers,
Nick.


-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list