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

Michael Chermside mcherm at mcherm.com
Wed Aug 2 12:53:38 CEST 2006


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".


Marc-Andre Lemburg writes:
> You often have a need for controlled rounding when doing
> financial calculations or [other reason snipped]

Hmm. Not in the banks _I_ have worked at! We *never* use binary
floating point for money. The decimal class is fairly useful in
that regard.

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.)

> People who care about the accuracy of calculations prefer binary,
> as it is a more accurate model.

Huh? That doesn't even make sense! A model is not inherently accurate
or inaccurate, it is only an accurate or inaccurate representation
of some "real" system. Neither binary nor decimal is a better
representation of either rational or real numbers, the first
candidates for "real" system I thought of. Financial accounting rules
tend to be based on paper-and-pencil calculations for which
decimal is usually a far better representation.

If you had said that binary floats squeeze out more digits of
precision per bit of storage than decimal floats, or that binary
floats are faster because they are supported by specialized hardware,
then I'd go along, but they're not a "better model".

-- Michael Chermside





More information about the Python-Dev mailing list