indutny / bn.js

BigNum in pure javascript
MIT License
1.19k stars 150 forks source link

Function `mod` gives incorrect results when the arguments are of opposite signs #288

Open barakman opened 2 years ago

barakman commented 2 years ago
+---------------+----------------+-----------------+---------------+
| first operand | second operand | expected output | actual output |
+---------------+----------------+-----------------+---------------+
|      -1       |       -2       |         -1      |       -1      |
+---------------+----------------+-----------------+---------------+
|      -1       |       +2       |         +1      |       -1      |
+---------------+----------------+-----------------+---------------+
|      +1       |       -2       |         -1      |       +1      |
+---------------+----------------+-----------------+---------------+
|      +1       |       +2       |         +1      |       +1      |
+---------------+----------------+-----------------+---------------+

When one operand is positive and the other operand is negative, the actual output is the negation of the expected output.


Expected output calculated via Python:

def format(x):
    return ('+' if x > 0 else '') + str(x)

def mod(x, y):
    print('(' + format(x) + ') % (' + format(y) + ') = ' + format(x % y))

mod(-1, -2)
mod(-1, +2)
mod(+1, -2)
mod(+1, +2)

Actul output calculated via JavaScript:

function format(x) {
    return (x.gtn(0) ? '+' : '') + x.toString();
}

function mod(x, y) {
    console.log('(' + format(x) + ') % (' + format(y) + ') = ' + format(x.mod(y)));
}

mod(new BN(-1), new BN(-2));
mod(new BN(-1), new BN(+2));
mod(new BN(+1), new BN(-2));
mod(new BN(+1), new BN(+2));