Integer division (was: Case-sensitivity... (was: Damnation!))

Bernhard Herzog herzog at online.de
Sun May 21 18:55:56 CEST 2000


François Pinard <pinard at iro.umontreal.ca> writes:

> Mathematicians' opinion surely has stronger value than mine here, but I do
> remember having written any generic algorithm in which it is meaningful
> that `i / j' sometimes mean truncating, sometimes not, depending on the
> type of the operands.  So spelling `int()' would not be a problem for me.
> 
> No doubt that I would not like having to revise existing code!  But also,
> as the current `i / j' gives access to useful hardware instruction, a new
> `int(i/j)' might incur unwelcome overhead for something that once was faster.

It just occurred to me that with the also planned unification of ints
and long ints the sematics of i / j where both are ints will have to be
carefully chosen. If you just cast them to floats before the division
you might end up with inaccurate results or overflows even if j is a
divisor of i.

If i/j does not do integer division, the only real solution seems to be
what Moshe Zadka proposed: introduction of a rational type with (long)
int numerator/denominator pairs.

I'm not sure I'd like that. It sounds like a serious performance hit.
You could easily end up carrying around a lot of precision that's not
needed in many applications.

This could be alleviated by introducing a special integer division
operator.

-- 
Bernhard Herzog   | Sketch, a drawing program for Unix
herzog at online.de  | http://sketch.sourceforge.net/



More information about the Python-list mailing list