Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
barakman opened this issue Jan 12, 2022 · 0 comments

Comments

@barakman
Copy link

barakman commented Jan 12, 2022

+---------------+----------------+-----------------+---------------+
| 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));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant