List: General Discussion « Previous MessageNext Message » From: Peter Brawley Date: February 25 2005 3:11pm Subject: Re: modulo operation View as plain text
```There are various interpretations of modulo. It's not simply
"remainder", eg see the discussion at
http://mathforum.org/library/drmath/view/52343.html including this chart

The relations between integer division, remainder, and modulus are
illustrated by the following table:

A   B   A/B   A rem B  A mod B     A     B    A/B   A rem B   A mod B

10   5    2       0        0      -10     5    -2       0         0
11   5    2       1        1      -11     5    -2      -1         4
12   5    2       2        2      -12     5    -2      -2         3
13   5    2       3        3      -13     5    -2      -3         2
14   5    2       4        4      -14     5    -2      -4         1

10  -5   -2       0        0      -10    -5     2       0         0
11  -5   -2       1       -4      -11    -5     2      -1        -1
12  -5   -2       2       -3      -12    -5     2      -2        -2
13  -5   -2       3       -2      -13    -5     2      -3        -3
14  -5   -2       4       -1      -14    -5     2      -4        -4

PB

Jigal van Hemert wrote:

>From: "Thomas Lenherr"
>
>Hi Thomas,
>
>
>
>>I just wanted to know if there is a special reason for the
>>mathematically incorrect implementation of the modulo-operation in mysql.
>>Using a correct modulo operation on a negative number would still result
>>in a positive number:
>>-1 % 2 == 1  (mysql: -1)
>>-5 % 3 == 1  (mysql: -2)
>>-1 % 4 == 3  (mysql: -1)
>>
>>(For the exact definition see
>>http://en.wikipedia.org/wiki/Modulo_operation )
>>
>>AFAIK most programming languages implement modulo in this wrong way
>>(except pascal i think), but I don't have a clou why it should stay that
>>way as I find this behaviour rather disturbing...
>>
>>
>
>I don't see any difference between the wikipedia definition and the MySQL
>implementation, especially since the article mentions that "what exactly
>constitutes the result of a modulo operation depends on the programming
>language and/or the underlying hardware."
>
>In the MySQL manual the results for negative numbers are not mentioned, nor
>defined.
>
>IMHO the examples you mention give correct results with MySQL:
> -1 / 2 = 0 remains: -1
> -5 / 3 = -1 remains: -2
> -1 / 4 = 0 remains: -1
>
>Regards, Jigal.
>
>
>
>

--
No virus found in this outgoing message.
Checked by AVG Anti-Virus.
Version: 7.0.300 / Virus Database: 266.4.0 - Release Date: 2/22/2005

```