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:

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;

No comments:

Post a comment