Quantcast
Channel: Answers for "Is there a modulus operator in Unity JavaScript?"
Viewing all articles
Browse latest Browse all 8

Answer by InoGamalinda

$
0
0
The % operator is the Remainder operator Example: > -3 % 3 -> 0>> -2 % 3 -> -2>> -1 % 3 -> -1 >> 0 % 3 -> 0 >> 1 % 3 -> 1>> 2 % 3 -> 2>> 3 % 3 -> 0 ---------- To do a real Modulo, you need to make or find a function similar to this: function modulo(dividend : int, divisor : int) : int { return (((dividend) % divisor) + divisor) % divisor; } Results: > -3 % 3 -> 0>> -2 % 3 -> 1>> -1 % 3 -> 2 >> 0 % 3 -> 0 >> 1 % 3 -> 1>> 2 % 3 -> 2>> 3 % 3 -> 0 Notice you get the same 0, 1, 2 cycle even when you reach the negative numbers. With this, you can get a consistent indexing for say like enumerators or arrays. ---------- Note: This assumption is from [Google calculator's -2 % 3 = 1][1] in contrast with JavaScripts -2 % 3 = -2. [1]: https://www.google.com/search?client=safari&rls=en&q=-2+%25+3&ie=UTF-8&oe=UTF-8

Viewing all articles
Browse latest Browse all 8

Latest Images

Trending Articles





Latest Images