% over math.fmod for IEEE remainder not working

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
ieeesub143
Prole
Posts: 1
Joined: Mon Feb 15, 2021 11:49 am

% over math.fmod for IEEE remainder not working

Post 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?
MrFariator
Party member
Posts: 548
Joined: Wed Oct 05, 2016 11:53 am

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

Post 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.
Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot], Bing [Bot] and 4 guests