Skip to content

Commit

Permalink
Merge pull request #419 from Unrud/fix-lua-modulo
Browse files Browse the repository at this point in the history
Fix lua modulo
  • Loading branch information
yuin committed Dec 10, 2022
2 parents 6581935 + 665ce4c commit 3323424
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
17 changes: 17 additions & 0 deletions _glua-tests/issues.lua
Original file line number Diff line number Diff line change
Expand Up @@ -387,3 +387,20 @@ function test()
assert(a == 1 and b == nil and c == nil)
end
test()

-- issue #412
-- issue #418
-- Conversion from symmetric modulo is incorrect.
function test()
assert(-2 % -2 == 0)
assert(-1 % -2 == -1)
assert(0 % -2 == 0)
assert(1 % -2 == -1)
assert(2 % -2 == 0)
assert(-2 % 2 == 0)
assert(-1 % 2 == 1)
assert(0 % 2 == 0)
assert(1 % 2 == 1)
assert(2 % 2 == 0)
end
test()
2 changes: 1 addition & 1 deletion _vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -840,7 +840,7 @@ func luaModulo(lhs, rhs LNumber) LNumber {
flhs := float64(lhs)
frhs := float64(rhs)
v := math.Mod(flhs, frhs)
if flhs < 0 || frhs < 0 && !(flhs < 0 && frhs < 0) {
if frhs > 0 && v < 0 || frhs < 0 && v > 0 {
v += frhs
}
return LNumber(v)
Expand Down
2 changes: 1 addition & 1 deletion vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1533,7 +1533,7 @@ func luaModulo(lhs, rhs LNumber) LNumber {
flhs := float64(lhs)
frhs := float64(rhs)
v := math.Mod(flhs, frhs)
if flhs < 0 || frhs < 0 && !(flhs < 0 && frhs < 0) {
if frhs > 0 && v < 0 || frhs < 0 && v > 0 {
v += frhs
}
return LNumber(v)
Expand Down

0 comments on commit 3323424

Please sign in to comment.