The REALLY bad thing about Python lists ..

Greg Ewing greg at cosc.canterbury.ac.nz
Tue May 23 22:33:10 EDT 2000


Russell Wallace wrote:
> 
> (Under what circumstances would you want to use % on negative numbers?
> I can't think of any.)

Here's a couple:

1) Consider a tile-based game in which the player walks around
on a grid of cells of size s by s. Given the pixel coordinates
(x, y) of the player, the cell coordinates are (x div s, y div s)
and the location within the cell is (x mod s, y mod s).

If div and mod are defined appropriately, it doesn't matter
where abouts in the grid the origin is. This may be useful,
for example if you want to simulate an infinite world by
automatically extending the grid when the player walks off
the edge. If coordinates are restricted to positive numbers,
it becomes somewhat more complicated to do that.

2) This one actually bit me in real life: Many years ago,
in Lightspeed Pascal on the Mac, I got into the habit of using
'Random mod n' to generate a number from 0 to n-1. Due
to the way mod was defined, this worked fine, even though
the Toolbox Random function returns a signed 16-bit integer.

Then they decided to change mod so that it complied with some
ANSI Pascal standard or other, and suddenly that didn't work any
more. I had to use 'abs(Random) mod n' instead, which I
found less than elegant.

-- 
Greg Ewing, Computer Science Dept,
+--------------------------------------+
University of Canterbury,	   | A citizen of NewZealandCorp, a	  |
Christchurch, New Zealand	   | wholly-owned subsidiary of USA Inc.  |
greg at cosc.canterbury.ac.nz	   +--------------------------------------+



More information about the Python-list mailing list