kchapelier / node-mathp

Math utility for node
MIT License
9 stars 1 forks source link

modulus #2

Closed megawac closed 9 years ago

megawac commented 9 years ago

It would be nice to have a proper math mod function (see http://javascript.about.com/od/problemsolving/a/modulobug.htm)

kchapelier commented 9 years ago

Some background informations :

http://wiki.ecmascript.org/doku.php?id=strawman:modulo_operator http://people.mozilla.org/~jorendorff/es6-draft.html#sec-applying-the-mod-operator

megawac commented 9 years ago

Yeah, the first thread the one I was looking for, we discussed this operator in quite some detail in https://github.com/ramda/ramda/issues/186 and I needed it today

kchapelier commented 9 years ago

Done some tests and research to clear up my understanding :

http://en.wikipedia.org/wiki/Modulo_operation

I'm in a bit of a bind as to decide what to implement to be honest. Euclidean seems to be the most consistent to me but it is also the most expensive. I might as well implement them all, name the functions accordingly and avoid making a "modulo" function as people may have a different opinion of how it should behave based on their background. Any thought on the matter ?

megawac commented 9 years ago

The most common implementation is the one from the blog post but I think itd be better to implement a version closer to the what would be expected mathematically.

kchapelier commented 9 years ago

Implemented euclideanModulo, flooredModulo and truncatedModulo.

Also ran some jsperf test to figure out if an alternative implementation without using % would be better (or worse). It turned out there are no real difference.

http://jsperf.com/modulo-comparaison

megawac commented 9 years ago

Good work, thanks