# Modulo operator produces incorrect results with negative operand(s).

21 messages
12
Open this post in threaded view
|

## Modulo operator produces incorrect results with negative operand(s).

 In Lua 5.3 "-3%5 == 2", according to my programmers calculator and several other programing languages I tried this expression in, the real result is -3.On the other hand "5%-3 == 2", so maybe the result is correct, and the bug is simply reversed operands if one is negative?I discovered this because I am adding tests (based on the official Lua tests) to my Go Lua VM, and one of the tests failed because the incorrect result is coded into the test.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 On 24/01/17 09:21 PM, Milo Christiansen wrote: > In Lua 5.3 "-3%5 == 2", according to my programmers calculator and > several other programing languages I tried this expression in, the > real result is -3. > > On the other hand "5%-3 == 2", so maybe the result is correct, and the > bug is simply reversed operands if one is negative? > > I discovered this because I am adding tests (based on the official Lua > tests) to my Go Lua VM, and one of the tests failed because the > incorrect result is coded into the test. First of all your modulo is not my modulo (or at least this was a thing back in C89... looks like it isn't in C99+). Secondly, -3%5 == 2 because floor(-3/5) = -1, and (-1)*5+2 = -5+2 = -3. See also: https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/https://github.com/sp614x/optifine/issues/288etc. This is an endless argument. Thirdly, in case you're still reading this for some unknown reason, RTFM always applies: http://www.lua.org/manual/5.3/manual.html#3.4.1"Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division)." -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Milo Christiansen The choice of sign seems to vary pretty widely, so IMHO portable code should not depend on it. Since Lua has implementations in many languages and aims to be fast it would be an unreasonable tradeoff to make the underlying operation conform to the original C implementation. Modulo is already among the slower operations, so the extra overhead is not necessarily worth it.On Tue, Jan 24, 2017 at 11:21 PM, Milo Christiansen wrote:In Lua 5.3 "-3%5 == 2", according to my programmers calculator and several other programing languages I tried this expression in, the real result is -3.On the other hand "5%-3 == 2", so maybe the result is correct, and the bug is simply reversed operands if one is negative?I discovered this because I am adding tests (based on the official Lua tests) to my Go Lua VM, and one of the tests failed because the incorrect result is coded into the test.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Soni "They/Them" L. Hmmm... So why does my programmers calculator, Go, JavaScript, etc, etc all return -3 in this case?Personally I think it's a bug, as "2" is not the correct result for that expression (in any language but Lua it seems).On Tue, Jan 24, 2017 at 6:40 PM, Soni L. wrote:On 24/01/17 09:21 PM, Milo Christiansen wrote: In Lua 5.3 "-3%5 == 2", according to my programmers calculator and several other programing languages I tried this expression in, the real result is -3. On the other hand "5%-3 == 2", so maybe the result is correct, and the bug is simply reversed operands if one is negative? I discovered this because I am adding tests (based on the official Lua tests) to my Go Lua VM, and one of the tests failed because the incorrect result is coded into the test. First of all your modulo is not my modulo (or at least this was a thing back in C89... looks like it isn't in C99+). Secondly, -3%5 == 2 because floor(-3/5) = -1, and (-1)*5+2 = -5+2 = -3. See also: https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/ http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/ https://github.com/sp614x/optifine/issues/288 etc. This is an endless argument. Thirdly, in case you're still reading this for some unknown reason, RTFM always applies: http://www.lua.org/manual/5.3/manual.html#3.4.1 "Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division)." -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 The way Lua does it may not be 100% wrong, but it is not the way an experienced programmer will expect it to work. IMHO this adds an unnecessary little speed bump in the way of good workflow.Wait, what am I saying, this is a language that thinks 1-based indexing is a good thing, forget I said anything. /sOn Tue, Jan 24, 2017 at 6:47 PM, Milo Christiansen wrote:Hmmm... So why does my programmers calculator, Go, JavaScript, etc, etc all return -3 in this case?Personally I think it's a bug, as "2" is not the correct result for that expression (in any language but Lua it seems).On Tue, Jan 24, 2017 at 6:40 PM, Soni L. wrote:On 24/01/17 09:21 PM, Milo Christiansen wrote: In Lua 5.3 "-3%5 == 2", according to my programmers calculator and several other programing languages I tried this expression in, the real result is -3. On the other hand "5%-3 == 2", so maybe the result is correct, and the bug is simply reversed operands if one is negative? I discovered this because I am adding tests (based on the official Lua tests) to my Go Lua VM, and one of the tests failed because the incorrect result is coded into the test. First of all your modulo is not my modulo (or at least this was a thing back in C89... looks like it isn't in C99+). Secondly, -3%5 == 2 because floor(-3/5) = -1, and (-1)*5+2 = -5+2 = -3. See also: https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/ http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/ https://github.com/sp614x/optifine/issues/288 etc. This is an endless argument. Thirdly, in case you're still reading this for some unknown reason, RTFM always applies: http://www.lua.org/manual/5.3/manual.html#3.4.1 "Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division)." -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Milo Christiansen > in any languageDid you look at the wikipedia page?For instance in C++ it is implementation defined.Also see C (pun unintended): http://stackoverflow.com/questions/11720656/modulo-operation-with-negative-numbersIMHO C's definition makes the most sense mathematically.As far as my first semester discrete math knowledge goes, this is the relevant equation:ka + r = n && 0 <= r < nwhere a and n are constantsThe important part is 0 <= r. This makes the solution unique for signed integers too and is what C seems to be based on.On Tue, Jan 24, 2017 at 11:47 PM, Milo Christiansen wrote:Hmmm... So why does my programmers calculator, Go, JavaScript, etc, etc all return -3 in this case?Personally I think it's a bug, as "2" is not the correct result for that expression (in any language but Lua it seems).On Tue, Jan 24, 2017 at 6:40 PM, Soni L. wrote:On 24/01/17 09:21 PM, Milo Christiansen wrote: In Lua 5.3 "-3%5 == 2", according to my programmers calculator and several other programing languages I tried this expression in, the real result is -3. On the other hand "5%-3 == 2", so maybe the result is correct, and the bug is simply reversed operands if one is negative? I discovered this because I am adding tests (based on the official Lua tests) to my Go Lua VM, and one of the tests failed because the incorrect result is coded into the test. First of all your modulo is not my modulo (or at least this was a thing back in C89... looks like it isn't in C99+). Secondly, -3%5 == 2 because floor(-3/5) = -1, and (-1)*5+2 = -5+2 = -3. See also: https://www.microsoft.com/en-us/research/publication/division-and-modulus-for-computer-scientists/ http://hisham.hm/2015/04/18/a-small-practical-example-where-luas-behavior-is-better-than-cs/ https://github.com/sp614x/optifine/issues/288 etc. This is an endless argument. Thirdly, in case you're still reading this for some unknown reason, RTFM always applies: http://www.lua.org/manual/5.3/manual.html#3.4.1 "Modulo is defined as the remainder of a division that rounds the quotient towards minus infinity (floor division)." -- Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Milo Christiansen > Wait, what am I saying, this is a language that thinks 1-based indexing is a > good thing, forget I said anything. /s
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Milo Christiansen >>The way Lua does it may not be 100% wrong, but it is not the way an experienced programmer will expect it to work. I disagree, an experienced programmer knows it varies across languages, and can even vary across implementations of those languages and thus safeguards against it.Knowing that it isn't what you'd expect is really all the info you need. It's why you program, to be in control. Complaining about it is unlikely to solve it.
Open this post in threaded view
|

## Re: Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Milo Christiansen Hallo, On 25/01/17 00:54, Milo Christiansen wrote: > > Wait, what am I saying, this is a language that thinks 1-based indexing > is a good thing, forget I said anything. /s > Already forgotten. -- -alex http://unendli.ch/
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negativeoperand(s).

 In reply to this post by Milo Christiansen >Wait, what am I saying, this is a language that thinks 1-based indexing is a good thing, forget I said anything. /s   Yes, what exactly are you saying? This *IS* a great thing!  And Lua isn’t the only language to do it right. (Yet another C-derived language infested programmer I suppose... Do you also count your fingers from 0 to 9?)   I expect your ‘friends’ will soon voice their support for you...
Open this post in threaded view
|

## Re: Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Alex Queiroz > Hmmm... So why does my programmers calculator, Go, JavaScript, etc, etc all return -3 in this case?> > Personally I think it's a bug, as "2" is not the correct result for that expression (in any language but Lua it seems).For what it's worth, among anyone who was a math major or took a course in algebra in college, the congruence class of -2 mod 5 is 3, and the idea that modulo should ever return negative numbers is an aberration inflicted on us by the makers of C some four decades ago. Just because everyone else timidly decided to follow in C's footsteps doesn't mean that this is the right thing to do, or even that it makes any sense at all.ChrisOn Wed, Jan 25, 2017 at 3:28 AM, Alex Silva wrote:Hallo, On 25/01/17 00:54, Milo Christiansen wrote: > > Wait, what am I saying, this is a language that thinks 1-based indexing > is a good thing, forget I said anything. /s > Already forgotten. -- -alex http://unendli.ch/
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 On 02/08/2017 02:40 PM, chris beck wrote: >> Hmmm... So why does my programmers calculator, Go, JavaScript, etc, > etc all return -3 in this case? >> >> Personally I think it's a bug, as "2" is not the correct result for > that expression (in any language but Lua it seems). > > For what it's worth, among anyone who was a math major or took a course > in algebra in college, the congruence class of -2 mod 5 is 3 [snip] It depends on additional restrictions of mod() results. Gerenally mod(n, m) is some function like   : get x, y such that   :   x * m + y == n,   :   int(x), int(y), abs(y) < abs(m)   : return y And further restrictions possible:   : x = min(possible x) // x leftmost from zero   => mod(-2, -5):  0 * -5 + -2 => -2         (1)   => mod( 2, -5): -1 * -5 + -3 => -3   => mod(-2,  5): -1 *  5 +  3 =>  3   => mod( 2,  5):  0 *  5 +  2 =>  2   : x = max(possible x) // x rightmost from zero   => mod(-2, -5):  1 * -5 +  3 =>  3         (2)   => mod( 2, -5):  0 * -5 +  2 =>  2   => mod(-2,  5):  0 *  5 + -2 => -2   => mod( 2,  5):  1 *  5 + -3 => -3   : y = min(possible y) // y leftmost from zero   => mod(-2, -5):  0 * -5 + -2 => -2         (3)   => mod( 2, -5): -1 * -5 + -3 => -3   => mod(-2,  5):  0 *  5 + -2 => -2   => mod( 2,  5):  1 *  5 + -3 => -3   : y = max(possible y) // y rightmost from zero   => mod(-2, -5):  1 * -5 +  3 =>  3         (4)   => mod( 2, -5):  0 * -5 +  2 =>  2   => mod(-2,  5): -1 *  5 +  3 =>  3   => mod( 2,  5):  0 *  5 +  2 =>  2 And even more:   : x = min(abs(possible x)) // x towards zero   => mod(-2, -5):  0 * -5 + -2 => -2         (5)   => mod( 2, -5):  0 * -5 +  2 =>  2   => mod(-2,  5):  0 *  5 + -2 => -2   => mod( 2,  5):  0 *  5 +  2 =>  2   : x = max(abs(possible x)) // x away zero   => mod(-2, -5):  1 * -5 +  3 =>  3         (6)   => mod( 2, -5): -1 * -5 + -3 => -3   => mod(-2,  5): -1 *  5 +  3 =>  3   => mod( 2,  5):  1 *  5 + -3 => -3   : y = min(abs(possible y)) // y towards zero   => mod(-2, -5):  0 * -5 + -2 => -2         (7)   => mod( 2, -5):  0 * -5 +  2 =>  2   => mod(-2,  5):  0 *  5 + -2 => -2   => mod( 2,  5):  0 *  5 +  2 =>  2   : y = max(abs(possible y)) // y away zero   => mod(-2, -5):  1 * -5 +  3 =>  3         (8)   => mod( 2, -5): -1 * -5 + -3 => -3   => mod(-2,  5): -1 *  5 +  3 =>  3   => mod( 2,  5):  1 *  5 + -3 => -3   /*     Due some reason results of (5) and (7) are equal.     Same for results (6) and (8).   */ So at least six different variants possible. Which one you prefer and why? -- Martin
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Martin On Thu, Feb 9, 2017 at 6:18 AM, Martin wrote: So at least six different variants possible. Which one you prefer and why? The division and modulo operations should come in pairs so that D = d “ (D div d) + (D mod d)The two obvious candidates for div are floor and truncate. Lua chooses floor. Common Lisp provides floor, truncate, ceiling, and round, with corresponding quotients and remainders.Folks interested in this topic should take a look at "The Euclidean Definition of the Functions div and mod" by Raymond T. Boute, University of Nijmegen, ACM Transactions on Programming Languages and Systems, Vol 14, No. 2, April 1992. Its abstract:The definitions of the functions div and mod in the computer science literature and in programming languages are either similar to the Algol or Pascal definition (which is shown to be an unfortunate choice) or based on division by truncation (T-definition) or division by flooring as defined by Knuth (F-definition). The differences between various definitions that are in common usage are discussed, and an additional one is proposed, which is based on Euclid’s theorem and therefore is called the Euclidean definition (E-definition). Its distinguishing feature is that 0 <= D mod d < abs(d) irrespective of the signs of D and d. It is argued that the E- and F-definitions are superior to all other ones in regularity and useful mathematical properties and hence deserve serious consideration as the standard convention at the applications and language level. It is also shown that these definitions are the most suitable ones for describing number representation systems and the realization of arithmetic operations at the architecture and hardware level. e
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 Thank you for clarifying, guys! Now I see that requirement of non-negative remainder is quite practical and well-binded with mathematical rings of natural numbers. Sadly (as I understand from similar discussions before) Lua implementation is heavily linked with ANSI C and Roberto generally not willing to introduce changes in logic that is different from ANSI C. Although I consider "%" and math.mod() behavior change worth it. -- Martin
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 It was thus said that the Great Martin once stated: > Thank you for clarifying, guys! > > Now I see that requirement of non-negative remainder is quite practical > and well-binded with mathematical rings of natural numbers. > > Sadly (as I understand from similar discussions before) Lua > implementation is heavily linked with ANSI C and Roberto generally not > willing to introduce changes in logic that is different from ANSI C. > > Although I consider "%" and math.mod() behavior change worth it.   Further fuel for the fire.  I did three different versions of mod---one in assembly (x86 32bit), C (x86 32bit) and Lua 5.3 (x86 32bit).  The results: [spc]lucy:/tmp>./mod 7 3 A: 2 1 C: 2 1 L: 2 1 [spc]lucy:/tmp>./mod -7 3 A: -2 -1 C: -2 -1 L: -3 2 [spc]lucy:/tmp>./mod 7 -3 A: -2 1 C: -2 1 L: -3 -2 [spc]lucy:/tmp>./mod -7 -3 A: 2 -1 C: 2 -1 L: 2 -1 [spc]lucy:/tmp>   -spc (Make of that what you will)
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Martin 2017-02-09 15:02 GMT+02:00 Martin <[hidden email]>: > Sadly (as I understand from similar discussions before) Lua > implementation is heavily linked with ANSI C and Roberto generally not > willing to introduce changes in logic that is different from ANSI C. Roberto might like to speak for himself on this, but IMHO the above comment oversimplifies his position.
Open this post in threaded view
|

## Re: Modulo operator produces incorrect results with negative operand(s).

 In reply to this post by Martin On Thu, Feb 9, 2017 at 2:02 PM, Martin wrote:> Sadly (as I understand from similar discussions before) Lua implementation is heavily linked with ANSI C and Roberto generally not willing to introduce changes in logic that is different from ANSI C.ANSI C usually means C89, where the division and modulo operators were implementation defined when either operand were negative. In Lua, on the other hand, these are not implementation defined. In more modern C standard, these are also not implementation defined, albeit different from Lua. So, in this particular case, Lua does not follow "ANSI" C at all.Cheers,V.