Page 1 of 1

% over math.fmod for IEEE remainder not working

Posted: Mon Feb 15, 2021 12:08 pm
by ieeesub143
Hi. Someone said that you don't need math.fmod but you should use % for IEEE remainder.
IEEE 754 specifies that x % Infinity should return x, for the % operator it returns NaN, for math.fmod it does it correctly.
IEEE 754 also specified that remainder(x, y) should be the remainder of x / y rounded to the nearest half even; for some reason -1 % 4 returns 4 instead of -1 as round(-1 / 4) is 0 and -1 - 0 is -1. math.fmod does it correctly?

Love2D Lua

Code: Select all

print(-1 % 5) -- 4
print(math.fmod(-1, 5)) -- -1
print(2 % math.huge) -- nan
print(math.fmod(2, math.huge)) -- 2
C#

Code: Select all

Console.WriteLine(Math.IEEERemainder(-1.0, 5.0)) // -1.0
Console.WriteLine(Math.IEEERemainder(2.0, Double.PositiveInfinity)) // 2.0
% is not working as an IEEE remainder and math.fmod is working fine?

Re: % over math.fmod for IEEE remainder not working

Posted: Mon Feb 15, 2021 3:10 pm
by MrFariator
This is more of an implementation detail with lua (and luajit), than it is with löve. I believe this lua mailing list post might be relevant.