Modulo Operator %%

I learned that the ReScript language does not provide a modulo operator, instead, you have to use a mod() function (though, I believe it's misnamed, and it actually does remainder as well). I liked that design decision, because it made me realize how little I used modulo, and that it really didn't need to be an operator. I certainly use it, and the operator is convenient, but probably not convenient enough to pass the syntax bar we normally place on operators.

However, given that remainder is already an operator in JavaScript, the question then becomes - when people use "%" in JavaScript, are they trying to use modulo or remainder? Is "%" a potential footgun? Even if we introduced a "Math.rem()" and "Math.mod()" function, will people try to use "%" anyways, because it's consise? If so, maybe it would be worth adding a %% operator, so that developers have an operator that's equally concise as "%", that isn't a footgun.

I don't know the answer to that. But, either way, I do think introducing functions would certainly be a good first step.

2 Likes

For what it’s worth, I have always planned to help out with a Math.modPow method after BigInt Math settles, along with other things like Math.popCount and Math.gcd, and I think Math.mod would also belong to that list. I don’t have the bandwidth to make a proper Math.mod proposal right now though—definitely not until BigInt Math settles.

3 Likes

First: I am a total beginner and coded just five programs with javascript (p5js) so far. I was linked here after failing to understand why % behaved (imo) so weirdly.

For what it's worth, I would really like a proper modulo function as I needed it in three of my programs so far. The main functions for me would be to:

-easily direct movement when the screen is a torus (+ moves right/down, - moves left/up)
-allow for easier implementation of mathematical structures such as principal ideal domains and irreducible polynomials

Mathematically I believe modulo has primacy over remainder, but I assume in computer science this is not so. For now I have my personal modulo function but it would be nice if it just exists.

Best, Kolm

3 Likes