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

Implement support for negative divisor in the BigNumber and Fraction implementation of function mod #3087

Closed
travellingprog opened this issue Oct 30, 2023 · 3 comments

Comments

@travellingprog
Copy link

travellingprog commented Oct 30, 2023

Describe the bug

gcd(x, y) fails if y is negative and if x or y is a BigNumber. The gcd(x, y) function internally calls the mod(x, y) function. Unfortunately, the mod(x, y) function has been set to fail when a BigNumber is involved, if the divisor y is negative.

Potential solution would be for gcd(x, y) to internally call mod(x, abs(y)) instead.

To Reproduce

const { BigNumber, gcd } = require('mathjs');
gcd(-2, BigNumber(12)); // 2  -- No issue
gcd(BigNumber(12), -2); // Uncaught Error: Cannot calculate mod for a negative divisor
@josdejong
Copy link
Owner

Good point. We recently improved the number implementation of the function mod (used by gcd) to support negative divisors, but haven't adjusted the BigNumber and Fraction implementations. It shouldn't be hard to adjust this and copy the number implementation of mod to BigNumber and Fraction.

Anyone interested in picking this up? Help would be welcome.

@josdejong josdejong changed the title gcd(x, y) fails if y is negative and if x or y is a BigNumber Implement support for negative divisor in the BigNumber and Fraction implementation of function mod Nov 1, 2023
@josdejong
Copy link
Owner

Fixed via 8679c07 (not yet published)

@josdejong
Copy link
Owner

Published now in v12.1.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants