List:General Discussion« Previous MessageNext Message »
From:Jigal van Hemert Date:February 25 2005 8:21am
Subject:Re: modulo operation
View as plain text  
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.

Thread
modulo operationThomas Lenherr25 Feb
  • Re: modulo operationJigal van Hemert25 Feb
    • Re: modulo operationPeter Brawley25 Feb
  • Re: modulo operationJigal van Hemert25 Feb
    • Re: modulo operationThomas Lenherr25 Feb