Modulo Operator

ECMAScript does not currently have a modulo operator. Instead, it has a remainder operator %. The result of remainder takes the sign of the dividend, not the divisor, so it does not have the nice limit-and-force-to-the-positive characteristics of modulo. Programs that use remainder in place of modulo are probably in error.

So it proposed that a modulo operator mod be added. The result of modulo takes the sign of the divisor, not the dividend.

The mod keyword is contextual, and backward-compatible only if productions in which it occurs have [no LineTerminator here] immediately to the left of mod.

Douglas Crockford 2011/02/13 00:51Brendan Eich 2011/03/23 20:12

We may want div too, for integer division. We could even consider divmod, returning a tuple:

alert(12 divmod 5)  // #[2, 2]

Brendan Eich 2011/02/16 05:15

I like that a lot. Assuming the grammar works out, I think it wins. There are other infix operators that could be added on a similar basis, like min and max.

Douglas Crockford 2011/02/16 23:44

I will write this up later today. The min and max cases seem better done via the existing and variadic Math.min and Math.max functions than infix operators (e.g., Math.min(1,2,3,0) returns 0), but we can hash that out elsewhere.

Brendan Eich 2011/02/28 21:29

 
strawman/modulo_operator.txt · Last modified: 2011/03/23 20:16 by brendan
 
Recent changes RSS feed Creative Commons License Donate Powered by PHP Valid XHTML 1.0 Valid CSS Driven by DokuWiki