I need help with modulus (%)

Hello, I’m trying to make a modulus function but adjust the values a little.

I know well what modulus is and what it does but I can’t find the equation it uses.
I found out that its something like this: a - b * math.floor(a / b) but this example is from other coding languages.

I don’t know what equation luau uses because it has some differences when using negative numbers with modulus to other languages.

Any help appreciated!

1 Like

I can’t find any documentation where the difference in behavior from other languages is explained.

I don’t think lua uses a different equation.

I ran this code and a%b == a - b * math.floor(a / b) was true in all a and b combinations (except when b = 0, both sides of the equation were nan (undefined) and nan == nan is false for some reason):

for a = -100, 100 do
	for b = -100, 100 do 
		if a%b ~= a - b * math.floor(a / b) then
			warn(a, b)
			print(a - b * math.floor(a / b))

I tested your code and it works like you said.
Weird, I think some of my numbers might have bugged out or something.

I think it has to do with this difference:

I would try looking at the open source Luau code to see how they implemented it in C++

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.