Closed LloydLS closed 1 year ago
Thanks for reporting. This is caused by the implementation of mod
using Math.floor
, see:
To fix this for real, we should pull in the higher level function floor of mathjs, which uses config.epsilon
. Maybe best is to keep the low level number function as is, copy the logic into mod.js, and adjust it there. Or maybe we should create a new internal helper function function modNumberInternal(x, y, floor) {...}
so you can pass a different implementation for floor
to it.
The same issue occurs with xgcdNumber
which also uses Math.floor
.
Anyone interested to help out improve mod
and maybe xgcd
? We should probably think it through a bit more before implementing a fix.
@LloydLS Can I work on it?
Thanks for your offer Madhu, that would be great.
Thanks for your PR #2940 @Madhu003 . I think we need to think through some steps before we can finish up this PR:
epsilon
epsilon
etc). mod
and gcd
). At this level we do have epsilon
so we can easily use that. modNumber
and gcdNumber
into mod.js
and gcd.js
, and then change the implementation to handle round-off errors in a similar way as function larger
using the util function nearlyEqual(x, y, epsilon)
.BigNumber
implementations of mod.js
and gcd.js
to handle round-off errors (similar to for example function larger
and other relational functions. What do you think?
Hi, @josdejong
I've gone through the issue and improved the precision logic of mod
using epsilon
(@LloydLS shouldn't have an issue anymore). But concerning your requirements, I need some clarifications:
.mod()
method inherited from the decimal.js
library used in BigNumber
implementations in mod.js
and gcd.js
?epsilon
?Expecting your reply.
Thanks for your PR Praise! About your two questions: I think the .mod()
method of decimal.js
works fine indeed. The most important reason that we would like to implement our own version is to make the behavior of mod
consistent with all related mathjs functions like floor
, largerEq
, etc. Those use epsilon
, and hence they give slightly different results for edge cases with round-off errors. Does that make sense you think?
On a side note: we do want to implement a separate epsilon
for bignumbers, since they obviously operate with a different precision than numbers, see #2608.
Fixed now in v11.11.1
Hi,
Describe the bug
mod()
sometimes returns an unexpected result, presumably due to round-off errors.To Reproduce
Please compare:
with
Version tested: 11.8.0
Best regards,