# Modulo Operator %% Classic List Threaded 26 messages 12
Open this post in threaded view
|

## Re: Re: Modulo Operator %%

 Isiah, thank you for composing this proposal. _______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Modulo Operator %%

 In reply to this post by Isiah Meadows-2 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. 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 [hidden email] https://mail.mozilla.org/listinfo/es-discuss
Open this post in threaded view
|

## Re: Modulo Operator %%

 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 > [hidden email] > https://mail.mozilla.org/listinfo/es-discuss_______________________________________________ es-discuss mailing list [hidden email] https://mail.mozilla.org/listinfo/es-discuss signature.asc (849 bytes) Download Attachment
Open this post in threaded view
|

## Re: Modulo Operator %%

Open this post in threaded view
|