fix(lua): compare float with int strictly in equality (#7611)
Co-authored-by: Peter Feerick <peter.feerick@gmail.com> Co-authored-by: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Peter Feerick
Claude Sonnet 5
parent
db18294f78
commit
34afae5a26
@@ -198,6 +198,46 @@ TEST(Lua, Switches)
|
||||
#endif
|
||||
}
|
||||
|
||||
TEST(Lua, testFloatIntegerEquality)
|
||||
{
|
||||
// 0.5 is not an integer, so it must not equal 0 (regression #7587)
|
||||
// both directions asserted explicitly so the intent is obvious at a glance
|
||||
luaExecStr("if 0.5 == 0 then error('0.5 == 0') end");
|
||||
luaExecStr("if not (0.5 ~= 0) then error('0.5 ~= 0') end");
|
||||
luaExecStr("if 0.50 == 0 then error('0.50 == 0') end");
|
||||
luaExecStr("if not (0.50 ~= 0) then error('0.50 ~= 0') end");
|
||||
luaExecStr("if 0.50 == 0.0 then error('0.50 == 0.0') end");
|
||||
// ... even when the value comes from a variable, as in the reported issue
|
||||
luaExecStr("local v = 0.50; if v == 0 then error('v == 0') end");
|
||||
luaExecStr("local v = 0.50; if not (v ~= 0) then error('v ~= 0') end");
|
||||
// negative non-integral floats must not equal integers either
|
||||
luaExecStr("if -0.5 == 0 then error('-0.5 == 0') end");
|
||||
luaExecStr("if not (-0.5 ~= 0) then error('-0.5 ~= 0') end");
|
||||
luaExecStr("if not (-0.5 < 0) then error('-0.5 < 0') end");
|
||||
luaExecStr("if -0.5 > 0 then error('-0.5 > 0') end");
|
||||
// integral floats still compare equal to their integer counterpart
|
||||
luaExecStr("if 1.0 ~= 1 then error('1.0 ~= 1') end");
|
||||
luaExecStr("if 0.0 ~= 0 then error('0.0 ~= 0') end");
|
||||
luaExecStr("if -1.0 ~= -1 then error('-1.0 ~= -1') end");
|
||||
// order comparisons on the same values must remain consistent
|
||||
luaExecStr("if not (0.5 >= 0) then error('0.5 >= 0') end");
|
||||
luaExecStr("if 0.5 <= 0 then error('0.5 <= 0') end");
|
||||
luaExecStr("if not (0.5 > 0) then error('0.5 > 0') end");
|
||||
luaExecStr("if 0.5 < 0 then error('0.5 < 0') end");
|
||||
luaExecStr("if not (0.50 >= 0) then error('0.50 >= 0') end");
|
||||
luaExecStr("if 0.50 <= 0 then error('0.50 <= 0') end");
|
||||
luaExecStr("if not (1.0 >= 1) then error('1.0 >= 1') end");
|
||||
luaExecStr("if not (1.0 <= 1) then error('1.0 <= 1') end");
|
||||
luaExecStr("if not (0.0 >= 0) then error('0.0 >= 0') end");
|
||||
luaExecStr("if not (0.0 <= 0) then error('0.0 <= 0') end");
|
||||
// mixed arithmetic must still yield floats; the fix only affects equality
|
||||
luaExecStr("if math.type(0.5 + 0) ~= 'float' then error('0.5 + 0') end");
|
||||
luaExecStr("if math.type(1.0 + 1) ~= 'float' then error('1.0 + 1') end");
|
||||
luaExecStr("if math.type(1 / 2) ~= 'float' then error('1 / 2') end");
|
||||
luaExecStr("if math.type(7.5 % 2) ~= 'float' then error('7.5 % 2') end");
|
||||
luaExecStr("if math.type(0.5 * 2) ~= 'float' then error('0.5 * 2') end");
|
||||
}
|
||||
|
||||
TEST(Lua, testLegacyNames)
|
||||
{
|
||||
MODEL_RESET();
|
||||
|
||||
Reference in New Issue
Block a user