Skip to content

Commit

Permalink
fix issue #63: the result of the MOD (%) operator is different from C…
Browse files Browse the repository at this point in the history
… Lua

for example: the result of (-125 + 1 + 8) % 64 should be 58 insted of -6
  • Loading branch information
xebecnan committed Jun 27, 2022
1 parent e989b22 commit 194eb31
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 4 deletions.
4 changes: 3 additions & 1 deletion Assets/UniLua/Coder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,9 @@ private static bool ConstFolding( OpCode op, ExpDesc e1, ExpDesc e2 )
e1.NumberValue = e1.NumberValue / e2.NumberValue;
break;
case OpCode.OP_MOD:
e1.NumberValue = e1.NumberValue % e2.NumberValue;
var v1 = e1.NumberValue;
var v2 = e2.NumberValue;
e1.NumberValue = v1 - Math.Floor(v1/v2) * v2;
break;
case OpCode.OP_POW:
e1.NumberValue = Math.Pow( e1.NumberValue, e2.NumberValue );
Expand Down
2 changes: 1 addition & 1 deletion Assets/UniLua/LuaObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,7 +375,7 @@ private static double O_Arith( LuaOp op, double v1, double v2 )
case LuaOp.LUA_OPSUB: return v1-v2;
case LuaOp.LUA_OPMUL: return v1*v2;
case LuaOp.LUA_OPDIV: return v1/v2;
case LuaOp.LUA_OPMOD: return v1%v2;
case LuaOp.LUA_OPMOD: return v1 - Math.Floor(v1/v2)*v2;
case LuaOp.LUA_OPPOW: return Math.Pow(v1, v2);
case LuaOp.LUA_OPUNM: return -v1;
default: throw new System.NotImplementedException();
Expand Down
7 changes: 5 additions & 2 deletions Assets/UniLua/VM.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,8 +324,11 @@ private void V_Execute()
{
var rkb = env.RKB;
var rkc = env.RKC;
if(rkb.V.TtIsNumber() && rkc.V.TtIsNumber())
{ ra.V.SetNValue(rkb.V.NValue % rkc.V.NValue); }
if(rkb.V.TtIsNumber() && rkc.V.TtIsNumber()) {
var v1 = rkb.V.NValue;
var v2 = rkc.V.NValue;
ra.V.SetNValue(v1 - Math.Floor(v1/v2)*v2);
}
else
{ V_Arith(ra, rkb, rkc, TMS.TM_MOD); }
env.Base = ci.BaseIndex;
Expand Down

0 comments on commit 194eb31

Please sign in to comment.