mathjs icon indicating copy to clipboard operation
mathjs copied to clipboard

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

Open josdejong opened this issue 2 years ago • 2 comments

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:

https://github.com/josdejong/mathjs/blob/09a044f9e1a12dbace174c6e83c46d0304c85d8d/src/plain/number/arithmetic.js#L159-L171

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

josdejong avatar Jul 20 '22 07:07 josdejong

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

gwhitney avatar Jul 20 '22 14:07 gwhitney

Ah, yes, thanks 😅

josdejong avatar Jul 20 '22 15:07 josdejong

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

mdeshpande12 avatar Oct 18 '23 11:10 mdeshpande12

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.

josdejong avatar Oct 18 '23 12:10 josdejong

@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

mdeshpande12 avatar Jan 05 '24 11:01 mdeshpande12

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

josdejong avatar Jan 10 '24 17:01 josdejong