Author Topic: modulo only works with integers?  (Read 2513 times)

But for anybody looking to a solution for the modulus problem:

Code: [Select]
function mMod(%num, %a) { //Supports floats unlike %
return ((%num / %a) - mFloor(%num / %a)) * %a;
}

I believe this is the most efficient way of doing this (well Port or Trinick is gonna come along and do stuff with binary operators or weird functions nobody's ever heard of, but that's true for any function you post, so whatever).

You should use % for integers where possible though, because that's like 20x faster.
That isn't correct tho

see the formula for modulus is:


Code: [Select]
function mMod(%a, %n)
{
return %a - %n * ~~(%a / %n);
}
is correct

~~ is the native torquescript equivalent of mFloor, it eliminates a function call to make things faster

EDIT FOR CLARIFICATION: What it really does is force a conversion to integer, then calls bitwise not on it twice (They cancel eachother out)
There are other ways of doing this too, if you really, really wanna get picky about performance. One of those ways is (expression) | 0
« Last Edit: February 13, 2015, 12:50:02 PM by Ipquarx »

~~ is the native torquescript equivalent of mFloor, it eliminates a function call to make things faster (What it really does, I believe, is cast whatever's in front of it to an int, so floats are rounded down and on integers it has no effect)

To clarify, the ~ symbol is the bitwise NOT operator. When you do ~~, you're negating the negation (~~T -> T) and along the way Torque drops the decimal places for some reason.

Any string where the initial part is parsable as a number is parsed as that number; once TorqueScript hits any non-numeric character, it stops parsing the string as a number.

==>echo("1.2whydoesthiswork" + 3.7);
4.9
ah ok wowe

That isn't correct tho

see the formula for modulus is:


Code: [Select]
function mMod(%a, %n)
{
return %a - %n * ~~(%a / %n);
}
is correct

That is the same formula, just in a different format;
((a / n) - floor(a / n)) * n
Multiply everything in the brackets by n
(a / n) * n - floor(a / n) * n
Simplify
a - floor(a / n) * n
Rearange
a - n * floor(a / n)

It's a lot more readable in that format.


Also the ~~ trick is pretty neat, might start using that..
« Last Edit: February 13, 2015, 12:54:54 PM by boodals 2 »

Also the ~~ trick is pretty neat, might start using that..
You can also use (%a / %n) | 0 if you wanna get really picky about performance (Uses one less torquescript instruction) but it looks much messier

negation

I read that as negaton and I was like woah that's a dope name for a negated address space.

Anyway, to check if a number is an integer I've always used %possibleInt + 0 $= %possibleInt because Torque will automatically convert a string to a number when adding 0 to it, so everything after the first alphabetical character in the string will be chopped off (for most strings this will be the entire thing resulting in 0, something like "4loko" will become 4) which will no longer be equal under a string check to the original variable. It has the luxury of avoiding any slow features like function calls and is simple enough. In theory you could use bitwise operators like ~~%possibleInt $= %possibleInt or %possibleInt | 0 $= %possibleInt or %possibleInt ^ 0 $= %possibleInt or whatever, but I like the simplicity of just adding zero. The string check is necessary because "hello" + 0 resolves to 0 and a number check with == would resolve the second operand (also "hello") into 0, and 0 == 0 so that won't work.

Here's a simple set of number type testing functions that I usually use:

function isInfinity(%n)
{
    return strcmp(%n, "-1.#INF") == 0 || strcmp("1.#INF") == 0;
}

function isNaN(%n)
{
    return strcmp(%n, "-1.#IND") == 0;
}

function isFloat(%n, %ignoreError)
{
    return %n $= (%n + 0) || (%ignoreError ? 0 : isInfinity(%n) || isNaN(%n));
}

function isInt(%n)
{
    return %n $= (%n | 0);
}

function isNumber(%n)
{
    return isFloat(%n) || isInt(%n);
}

function isTaggedString(%n)
{
    return getSubStr(%n, 0, 1) $= "\x01" && strlen(%n) > 1;
}

well Port or Trinick is gonna come along and do stuff with binary operators or weird functions nobody's ever heard of
Boodals called it though.