A use for integer quotients

David Eppstein eppstein at ics.uci.edu
Mon Jul 23 03:45:13 EDT 2001


In article <mailman.995866298.23496.python-list at python.org>,
 "Tim Peters" <tim.one at home.com> wrote:

> > I don't suppose it would be possible to go through some repository of
> > Python sources and figure out the relative numbers of divisions
> > of integer arguments that end up an int versus the ones that are
> > coerced to floats?
> 
> Guido did this for the Python library, and found three instances of "/" that
> would break.
> 
> > In my own code it's 100% int/int->int
> 
> Mine too -- but only in my integer code <wink>.  Curiously, I find I
> *usually* use divmod() even then, because I usually need the remainder too.

Ok, you convinced me to look more carefully at all the divisions in my 
recent small project, a program to generate web pages from collections of 
JPEGs.  I found:

3 instances of simplifying numbers prior to output (e.g. I want file size 
in kbytes rather than bytes).

1 instance of a magic formula to convert focal length to 35mm equivalents.  
You might argue that floating point is more appropriate here, but I want to 
see integers in the output and the formula was tested by comparing against 
the camera manufacturer's software which only displays integer output.

6 instances of determining transformed image sizes:
newX = (oldX * targetSize)/max(oldX,oldY) etc.  I don't know whether PIL 
allows image.resize() to take float arguments, whether it throws an 
exception, or whether it just does something bogus.  I'd hope for an 
exception, float doesn't make much sense in that context.

...and lots of slashes in HTML tags inside strings making it harder to find 
the actual division operators.

Does my short program really have three times as many integer divisions as 
the whole Python library?  Not even counting the divisions I'm sure are 
lurking in PIL?  Also note that in none of those instances do I care about 
remainders nor want to uglify my formulas by divmod(a,b)[0].

The magic formula is the one that's least likely to make sense a couple 
years later when this issue starts to bite.  And, I won't actually see any 
warnings because the console window is disabled in my compiled applet due 
to a bad interaction with W that causes programs that don't disable the 
console to become immortal.  So, anyone who I might have passed the program 
on to will one day suddenly see ugly floats in their generated web pages, 
if the program still works at all, and won't have any clue why or how to 
fix it.
-- 
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