• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

Why does Modulo goes negative?

Kyon

Member
Hi everyone, I was wondering why Modulo allows something to go negative.
Like:
400 % 360 = 40
but
-400 & 360 = -40

What if I want my modulo results to always be above 0.
So always between 0 and 360 in this case.



Kyon.


PS: I mean I know I can do it by making a seperate variable and check if it's below 0 and then make it positive. But I was wondering if there was a simpler mathematical solution
 
Last edited:

TsukaYuriko

☄️
Forum Staff
Moderator
Because that's literally how the operator works. Divide by 360 until the number is no longer divisible without fractions. Return the rest. The rest is negative, so the result is negative.

Use abs if you want it to always be positive.
 

TheouAegis

Member
It's even negative on my Windows calculator...

-A mod B = -C
A mod B = C

Like Tsuriko said, if the part you are working with (A) is negative, then what you'll have left over is negative; if the part you are working with is positive, then you will have a positive amount left over. It doesn't matter if B is positive or negative, all that matters is whether A is positive or negative. If you tried to divide 400 parts of anti-matter up into 360 containment units, you would have 40 parts of anti-matter left over, not 40 parts of normal matter.
 

Kyon

Member
Because that's literally how the operator works. Divide by 360 until the number is no longer divisible without fractions. Return the rest. The rest is negative, so the result is negative.

Use abs if you want it to always be positive.
It's even negative on my Windows calculator...

-A mod B = -C
A mod B = C

Like Tsuriko said, if the part you are working with (A) is negative, then what you'll have left over is negative; if the part you are working with is positive, then you will have a positive amount left over. It doesn't matter if B is positive or negative, all that matters is whether A is positive or negative. If you tried to divide 400 parts of anti-matter up into 360 containment units, you would have 40 parts of anti-matter left over, not 40 parts of normal matter.

Ok thanks both, makes sense!!
I have never even used "Abs" so that's a new one, thanks for that tip, it's perfect.



Kyon.
 

NightFrost

Member
This has been discussed here before. It is because GML modulo (and that of certain other programming languages) does not work on the basis of Euclidean division, meaning the result can be negative instead of always positive. An always-positive modulo would be useful for building counters that can circle around - for example, for 360 degree variable - but if that's your goal, you'll have to write your own script. Using abs will not cut it. For example the "correct" value for -10 mod 360 would be 350, not abs(-10). You must do it like this.
 

Kyon

Member
This has been discussed here before. It is because GML modulo (and that of certain other programming languages) does not work on the basis of Euclidean division, meaning the result can be negative instead of always positive. An always-positive modulo would be useful for building counters that can circle around - for example, for 360 degree variable - but if that's your goal, you'll have to write your own script. Using abs will not cut it. For example the "correct" value for -10 mod 360 would be 350, not abs(-10). You must do it like this.
Yeah I figured today that abs didn't work indeed. I was really stressing about it although the reason is quite logical.
Anyway, I fixed it by checking if the original amount was lower than 0, and if so add 360 to it.

Thanks for all the help ;)
 
Top