A use for integer quotients
David Eppstein
eppstein at ics.uci.edu
Mon Jul 23 12:12:56 EDT 2001
In article <3B5C3713.A5201C5A at letterror.com>,
Just van Rossum <just at letterror.com> wrote:
> 1) only one of the / operands need to be a float to force a float
> outcome, and 2) there is a round() builtin function. Now, if we
> initialize k to a float, we can write it like so:
>
> int(round(bytes/k))
>
> I really don't see how the int examples you gave are in any way superior.
In the case under discussion it doesn't matter (the rounded number is just
an approximation for human discussion) but it is just wrong to me to
confuse integers and floating point numbers to the extent that you
gratuitously convert from int to float and back when you don't need to and
think it an improvement. Integers are exact. Floating point numbers are
approximate. Integers allow rearrangements like (a+b)+c=a+(b+c), floating
point numbers don't. If you need your numbers to be computed exactly, and
your programming language turns them into floats behind your back, it will
cause bugs.
--
David Eppstein UC Irvine Dept. of Information & Computer Science
eppstein at ics.uci.edu http://www.ics.uci.edu/~eppstein/
More information about the Python-list
mailing list