Modulo Operator %%
kdex
kdex at kdex.de
Fri Aug 16 16:09:44 UTC 2019
Reply inline.
On Friday, August 16, 2019 5:50:40 PM CEST peter miller wrote:
> Hi,
>
> +1 for _at_least_ having `Math.mod()`
>
> And here's a reason why:
>
> The code `mod = (x,y) => ( ( x % y ) + y ) % y` is not only slow but
> inaccurate. For example, for `mod( -249825144445569.97,
> -2022673516699079.8)` gives `-249825144445569.75` (Chrome Version
> 77.0.3865.35) instead of `-249825144445569.97`
>
> I then switched to `mod = (x,y) => x - Math.floor( x / y ) * y` which
> gives an accurate answer, and is faster.
>
> But, on a hunch that flow control would be faster than arithmetic, I
> devised this:
>
> ```
> mod = (x,y) => {
> const z = x % y;
> return (y >= 0 && z >= 0 ) || ( y < 0 && z <= 0 ) ? z : y + z;
> }
> ```
> And that is _sometimes_ the fastest approach -- assuming I haven't screwed
> up the maths; cf https://jsbench.me/lrjze7oqv9/2 Although it does seem to
> depend on the numbers - with the `Math.floor()` approach sometimes coming
> out faster.
>
> And this all neglects BigInts, Infinites, NaNs, and signed zeroes.
Most, if not all, functions on the `Math` prototype can't handle `BigInt`, but
that's a separate issue.
While it would be nice if `Math.mod` supported that, another `Math` proposal
could spec that along with fixes for all the other methods.
For now, it should be good enough if we had a modulo operation at all.
>
> So this isn't a trivial function to polyfill. And, for that reason alone,
> it should be in the standard. (And if we get it in Math, and convince
> people to use it, then the pressure for a more ergonomic solution will
> increase.)
>
> Peter
> _______________________________________________
> es-discuss mailing list
> es-discuss at mozilla.org
> https://mail.mozilla.org/listinfo/es-discuss
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: This is a digitally signed message part.
URL: <http://mail.mozilla.org/pipermail/es-discuss/attachments/20190816/581087dd/attachment.sig>
More information about the es-discuss
mailing list