Re: modulo operation
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. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: modulo operation
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 from the 1983 Ada manual: 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 BA/B A rem B A mod B 10 52 00 -10 5-2 0 0 11 52 11 -11 5-2 -1 4 12 52 22 -12 5-2 -2 3 13 52 33 -13 5-2 -3 2 14 52 44 -14 5-2 -4 1 10 -5 -2 00 -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 -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: modulo operation
From: Peter Brawley 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 from the 1983 Ada manual: The article at wikipedia already mentioned multiple interpretations. My point was that while Thomas wrote that modulo in MySQL was flawed and pointed to the wikipedia page to prove his point, the wikipedia page in question was not contradicting the MySQL implementation at all. From: Thomas Lenherr (For the exact definition see http://en.wikipedia.org/wiki/Modulo_operation ) It is strange for mathematical operations/functions to depend upon interpretation or implementation. If there is anything in this world that is properly defined, it must be mathematics. Maybe Thomas (or you) can file a bugreport with the suggestion to add a mathematically correct modulo function; I don't think the current modulo function/operator should be altered, since this might break a lot of queries out there! Regards, Jigal. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
Re: modulo operation
Hi Jigal, Hi Peter, first of all: it's true, the wikipedia-site is not contradicting the mysql-implementation, it seems i didn't read it too carefully... The guy who invented the modulo-operation was G.F. Gauss in his book Disquisitiones Arithmeticae which was published in 1801. His definition is documented here: http://en.wikipedia.org/wiki/Modular_arithmetic (section congruence relation). He defined the equivalence operation modulo as following: We call two integers a, b congruent modulo n if their difference is divisible by n, or equivalently: if they leave the same remainder when divided by n. This sentence seems to be the source of your different opinion: The difference of a and b is always = 0 therefore their difference mod n would always be a positive integer. The second part defines the relation in slightly other way, using the remainder. As pointed out by the table pasted by Peter remainder and modulo is not always exactly the same, but if the remainder by any chance can be negative, the following line is not more always working for example in an if-statement: a mod n == b mod n because if exactly one of a and b is negative so are the remainders (if they're not multiples of n), ex: -3 mod 2 != 1 mod 2, as -3 mod 2 == -1 but abs(-3 - 1) mod 2 == abs(-4) mod 2 == 4 mod 2 == 0 were we get a contradiction... Thomas Jigal van Hemert wrote: From: Peter Brawley 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 from the 1983 Ada manual: The article at wikipedia already mentioned multiple interpretations. My point was that while Thomas wrote that modulo in MySQL was flawed and pointed to the wikipedia page to prove his point, the wikipedia page in question was not contradicting the MySQL implementation at all. From: Thomas Lenherr (For the exact definition see http://en.wikipedia.org/wiki/Modulo_operation ) It is strange for mathematical operations/functions to depend upon interpretation or implementation. If there is anything in this world that is properly defined, it must be mathematics. Maybe Thomas (or you) can file a bugreport with the suggestion to add a mathematically correct modulo function; I don't think the current modulo function/operator should be altered, since this might break a lot of queries out there! Regards, Jigal. -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]
modulo operation
Hi, 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... Cheers, Thomas -- MySQL General Mailing List For list archives: http://lists.mysql.com/mysql To unsubscribe:http://lists.mysql.com/[EMAIL PROTECTED]