From 93c8906ca2f77270054edb9f7669ca9b06f80b6f Mon Sep 17 00:00:00 2001 From: daurnimator Date: Sun, 21 May 2017 20:46:54 +1000 Subject: src/lmathlib.js: fix fmod: plain javascript % operator has correct behaviour --- src/lmathlib.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'src/lmathlib.js') diff --git a/src/lmathlib.js b/src/lmathlib.js index 8c7b8a5..3e4486a 100644 --- a/src/lmathlib.js +++ b/src/lmathlib.js @@ -203,15 +203,15 @@ const math_type = function(L) { const math_fmod = function(L) { if (lua.lua_isinteger(L, 1) && lua.lua_isinteger(L, 2)) { let d = lua.lua_tointeger(L, 2); - if (Math.abs(d) + 1 <= 1) { - lauxlib.luaL_argcheck(L, d !== 0, 2, lua.to_luastring("zero", true)); - lua.lua_pushinteger(L, 0); + /* no special case needed for -1 in javascript */ + if (d === 0) { + lauxlib.luaL_argerror(L, 2, lua.to_luastring("zero", true)); } else - lua.lua_pushinteger(L, lua.lua_tointeger(L, 1) % d); + lua.lua_pushinteger(L, (lua.lua_tointeger(L, 1) % d)|0); } else { let a = lauxlib.luaL_checknumber(L, 1); let b = lauxlib.luaL_checknumber(L, 2); - lua.lua_pushnumber(L, Number((a - (Math.floor(a / b) * b)).toPrecision(8))); + lua.lua_pushnumber(L, a%b); } return 1; }; -- cgit v1.2.3-54-g00ecf