R
Rackover
Guest
-1%5 is supposed to give 4, not -1
Why does Game Maker say otherwise ?
//customMod(inNum, modNum)
{
var inNum = argument0;
var modNum = argument1;
if inNum >= 0
return inNum mod modNum;
if inNum < 0
return (inNum * -1) mod modNum;
}
This doesn't look correct to me. The sample case with -1 mod 5 with your script will return 1 instead of 4.If you want to feed mod a negative number (like -1 and have it to return 4) just make a small script like:
//customMod(inNum, modNum)
{
var inNum = argument0;
var modNum = argument1;
if inNum >= 0
return inNum mod modNum;
if (inNum < 0) && (inNum * -1 < modNum) {
return modNum - ((inNum * -1) mod modNum )
} else {
return (inNum * -1) mod modNum;
}
/// @desc mod2(a,b)
/// @arg a
/// @arg b
var a = argument0;
var b = argument1;
return (b + (a mod b)) mod b;
-1 mod 5 is 4.You guys are right, sorry i didnt test my script.
It's debatable on what the correct output of -1 mod 5 would actually be, I would think it could possibly be 6. Let me explain, say you stock 5 items. Someone orders 6. You send him 5 and owe him 1. But interestingly the remainder that you now have to make or aquire is 6 to restock to inventory of 5.
But on the other hand the remainder is actually -1 because you promised 6 and had 5.
And on the 3rd hand (for those that have 3 hands lol) the remainder would be 1 because that's the remainder you need to complete your order.
This topic is frustrating haha
-(1 % 5)
/// Nmod(n, a);
var n = argument[0];
var a = argument[1];
return n - floor(n / a) * a;