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

Make the behavior of mod for BigNumber and Fraction consistent with the behavior of number #2630

Closed
josdejong opened this issue Jul 20, 2022 · 6 comments
Labels
bug good first issue Relatively localized/straightforward changes that would provide a good introduction to mathjs code help wanted

Comments

@josdejong
Copy link
Owner

josdejong commented Jul 20, 2022

Discovered during the discussion in #2617: in mathjs, mod is implemented the mathematical way, which differs a bit from the typical programmatical way: math.mod(22, 0) returns 22. However, this behavior is not yet implemented for BigNumber and Fraction. We should make that behavior consistent:

math.mod(22, 0)                                  // 22 as expected
math.mod(math.bignumber(22), math.bignumber(0))  // NaN, should be BigNumber 22
math.mod(math.fraction(22), math.fraction(0))    // NaN, should be Fraction 22

The behavior of mod for numbers is implemented as follows, we should do the same for BigNumber and Fraction:

export function modNumber (x, y) {
if (y > 0) {
// We don't use JavaScript's % operator here as this doesn't work
// correctly for x < 0 and x === 0
// see https://en.wikipedia.org/wiki/Modulo_operation
return x - y * Math.floor(x / y)
} else if (y === 0) {
return x
} else { // y < 0
// TODO: implement mod for a negative divisor
throw new Error('Cannot calculate mod for a negative divisor')
}
}

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

@gwhitney
Copy link
Collaborator

[Took the liberty of fixing the zeros in the issue statement to be 22s]

@josdejong
Copy link
Owner Author

Ah, yes, thanks 😅

@gwhitney gwhitney added the good first issue Relatively localized/straightforward changes that would provide a good introduction to mathjs code label Oct 4, 2023
@mdeshpande12
Copy link

@josdejong @gwhitney if this is still open I'd like to work on this issue.

@josdejong
Copy link
Owner Author

Thanks @mdeshpande12 , your help is very welcome!

A quick check shows that this behavior seems to be consistent for BigNumber now, but not yet for Fraction. It would be good though to have a more thorough look at both the behavior of BigNumber and Fraction.

@mdeshpande12
Copy link

@josdejong I did a little exploration of myself and it turns out, behaviour is consistent for both BigNumber, Fraction as that of the number in the latest version. Details are as follows:

Latest Version of mathjs: 12.2.1
Version used for comparison: 10.6.4

Please refer to the attached screenshots:
Screenshot 2024-01-05 at 5 14 13 PM

Screenshot 2024-01-05 at 5 14 54 PM

Result: I believe this issue is resolved with the latest version, and can be closed

@josdejong
Copy link
Owner Author

Ha, you're right! I did another quick test too, and the original issue doesn't occur anymore. That's good news :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug good first issue Relatively localized/straightforward changes that would provide a good introduction to mathjs code help wanted
Projects
None yet
Development

No branches or pull requests

3 participants