lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Rici wrote:

> Is there a good reason why % is implemented in terms of
> floor rather than fmod? The following seems curiously
> inconsistent to me:

I made a chart comparing math.mod(-6 through 6, -3 through
3) to (-6 through 6) % (-3 through 3).  When the two give
different results, the math.mod result is to the left of the
colon.

     -3     -2     -1   0     1   2      3
   +------+------+----+-----+---+------+------+
-6 | 0    | 0    | 0  | nan | 0 | 0    | 0    |
-5 | -2   | -1   | 0  | nan | 0 | -1:1 | -2:1 |
-4 | -1   | 0    | 0  | nan | 0 | 0    | -1:2 |
-3 | 0    | -1   | 0  | nan | 0 | -1:1 | 0    |
-2 | -2   | 0    | 0  | nan | 0 | 0    | -2:1 |
-1 | -1   | -1   | 0  | nan | 0 | -1:1 | -1:2 |
0  | 0    | 0    | 0  | nan | 0 | 0    | 0    |
1  | 1:-2 | 1:-1 | 0  | nan | 0 | 1    | 1    |
2  | 2:-1 | 0    | 0  | nan | 0 | 0    | 2    |
3  | 0    | 1:-1 | 0  | nan | 0 | 1    | 0    |
4  | 1:-2 | 0    | 0  | nan | 0 | 0    | 1    |
5  | 2:-1 | 1:-1 | 0  | nan | 0 | 1    | 2    |
6  | 0    | 0    | 0  | nan | 0 | 0    | 0    |
   +------+------+----+-----+---+------+------+

I personally like the % behavior better.  On at least one
occasion, I've had to hack around the math.mod behavior.

-- 
Aaron