An operator is far more concise than a function call, and is likely to see greater use. It also aligns better with peoples' intuition on what the "modulus" is, avoiding subtle bugs like in `isOdd = x => x % 2 === 1` (example from https://en.wikipedia.org/wiki/Modulo_operation#Common_pitfalls - try passing a negative to it). And given this one is high value (see above) and *very* low cost (it can literally desugar to `(x % y + y) % y`), I feel it does meet that bar.
> It would be interesting to hear the feedback of those that use regularly > powers, whether the benefit was clear (personally, I almost never use either > `Math.pow()` or `**`, so that I can’t say anything). It has enough benefit I've seen CoffeeScript users default to `%%` and only using `%` when they explicitly want the dividend-dependent semantics. And engines with a native `%%`, if they can detect the operands are always non-negative, can optimize it to `%` pretty easily. It's better *enough* that you'd likely start seeing some partially legitimate FUD spread about the standard `%`. One other added benefit of using divisor-dependent modulo is that `x %% (2**n)`, where `x` and `n` are integers and `n >= 0`, could always be safely rewritten to `x & (2**n - 1)` while still preserving semantics, but `x % (2**n)` does *not* have this property. For example: - `-1 %% (2**1)` → `-1 %% 1` → `1` - `-1 & (2**1 - 1)` → `-1 & 1` → `1` - `-1 % (2**1)` → `-1 % 2` → `-1` BTW, I literally tested all three of these in Chrome's devtools console, using my `x %% y` → `(x % y + y) % y` desugaring. As for a native implementation and the spec, I'd recommend just doing `copysign(fmod(x, y), y)` instead to retain precision. > At least one disadvantage of an operator over a function, is that you have to > think about precedence. The problem is exacerbated in JS, because (following > some other languages) the unary minus has an uncanny high precedence level, > confusingly very different than the one of the binary minus; so that, after > having designed `**`, it was realised at the last minute that `-a**b` would > be dumbly interpreted as `(-a)**b` instead of `-(a**b)` or `0-a**b`, as > anybody who would be likely to actually use the operator would expect. (That > particular issue was resolved in a hurry by making the parenthesis-left form > a syntax error.) I doubt this would happen with `%%`. It's similar enough to the existing `%` in concept that most would expect it to have the same precedence. With `**`, there was a very unique issue with it: there were people actually *reading* it both ways, and even a language (Python) that interprets `-a ** b` and `-a**b` *differently* in light of that (as `(-a) ** b` and `-(a ** b)` respectively). That's not a concern at all with most operators, so it doesn't apply to most new operator proposals. ----- Isiah Meadows [email protected] www.isiahmeadows.com On Thu, Aug 15, 2019 at 2:40 AM Claude Pache <[email protected]> wrote: > > > > Le 12 août 2019 à 22:00, Matthew Morgan <[email protected]> a écrit : > > > JS needs a modulo operator. It currently has the remainder operator `%` > > which works in most cases except for negative values. I believe the the > > `%%` would work great and be easy to remember. > > > > let x = (-13) %% 64; > > is equivalent to > > let x = ((-13 % 64) + 64) % 64; > > > Is there a strong advantage of an `%%` operator over a `Math.mod()` function? > There is the precedent of the `**` operator implemented as alternative of > `Math.pow()` few years ago. It would be interesting to hear the feedback of > those that use regularly powers, whether the benefit was clear (personally, I > almost never use either `Math.pow()` or `**`, so that I can’t say anything). > > At least one disadvantage of an operator over a function, is that you have to > think about precedence. The problem is exacerbated in JS, because (following > some other languages) the unary minus has an uncanny high precedence level, > confusingly very different than the one of the binary minus; so that, after > having designed `**`, it was realised at the last minute that `-a**b` would > be dumbly interpreted as `(-a)**b` instead of `-(a**b)` or `0-a**b`, as > anybody who would be likely to actually use the operator would expect. (That > particular issue was resolved in a hurry by making the parenthesis-left form > a syntax error.) > > —Claude > > _______________________________________________ > es-discuss mailing list > [email protected] > https://mail.mozilla.org/listinfo/es-discuss _______________________________________________ es-discuss mailing list [email protected] https://mail.mozilla.org/listinfo/es-discuss

