Friday, 16 June 2017

how to get real modulus by javascript



in Js, the % is still to get the reminder, when your value is positive, it is ok, when it is nagitive, the value is "wrong".

Please have a look of the mod function in java BigInteger:
https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#mod(java.math.BigInteger)

Even MDN said they may implement the real modulus.




here is a solution before the official implementation

Number.prototype.mod = function(n) {
    return ((this%n)+n)%n;
};

https://stackoverflow.com/questions/4467539/javascript-modulo-not-behaving
http://javascript.about.com/od/problemsolving/a/modulobug.htm

No comments:

Post a comment