Fixing up the mod function to produce only positive results?



You might try nummod; if load("nummod") doesn't work, get
nummod(4).zip from http://www.unk.edu/facstaff/profiles/willisb/

Here is the user documentation:

nummod(x,y)

  If x and y are real numbers and y is nonzero, return x - y * floor(x /
  y).
  Further for all real x, we have nummod(x,0) = x. For a discussion of
  the definition nummod(x,0) = x, see Section 3.4, of "Concrete
  Mathematics,"
  by Graham, Knuth, and Patashnik. The function nummod(x,1)
  is a sawtooth function with period 1 with nummod(1,1) = 0 and
  nummod(0,1) = 0.

  To find the principal argument (a number in the interval (-%pi,%pi]) of a
  complex number, use the function x |-> %pi - nummod(%pi-x,2*%pi), where
  x is an argument.

  When x and y are constant expressions (10 * %pi, for example), nummod
  uses the same big float evaluation scheme that floor and ceiling uses.
  Again, it's possible, although unlikely, that nummod could return an
  erroneous value in such cases.

  For nonnumerical arguments x or y, nummod knows several simplification
  rules:

  (%i1) nummod(x,0);
  (%o1) x
  (%i2) nummod(a*x,a*y);
  (%o2) a*nummod(x,y)
  (%i3) nummod(0,x);
  (%o3) 0

Barton