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
math.floor returns apparently wrong value for BigNumbers; possibly implement distinct epsilon for BigNumber operations? #2608
Comments
The reason is that the functions math.equal(0.99999999999999, 1) // true, because it is 'nearly equal' This behavior is configurable with the config value To solve this, you can configure a smaller epsilon, like: math.config({ epsilon: 1e-60 })
math.floor(math.bignumber("1234567890123456.9951").mul( math.bignumber(100)),0).toString()
// '123456789012345699' as expected |
I misunderstood the function usage , Thank you for your answer , it helps me a lot ^____^ ^____^ |
Well, it's not your fault really, it's simply not working ideal. I've documented this in the docs now: 297d2e0 But I have the feeling that we should think this through and come up with a solution that prevents this issue in the same place. Both Anyone interested into thinking this though and come up with a solution? |
I use java more , I suggestion can reference design of java。 you say besides maybe we can providing one boole type switch to close the approximate comparison feature。 I am not very proficient in JS, sad not to provide help code ( ̄︶ ̄)↗ |
I think mathjs has more or less the same , it has a It makes sense to introduce a config option to enable/disable nearly equal comparisons, thanks for your suggestion. |
I'd like to work on that |
Thanks @jakubriegel for picking this up! I think we should make this idea more concrete. Can you do a proposal? |
I want to first investigate the implementation and check if it can be done via some already present config or is there a need for custom logic |
I think the problem is that complicated and see three options:
I did a quick research on how it is done in NumPy and it turns out they do allows users to set the epsilon in any way. |
Thanks for thinking this through. I have a preference for the first option, implementing a separate nearlyEqual(x, y, config.epsilon)
bigNearlyEqual(x, y, config.epsilon) // <-- here we can use a different epsilon Thinking aloud here: we could go for two separate options |
Why Math.floor is changing its behaviour with value '4.89' > any one can explain to understand it |
If you execute EDIT: when using mathjs, the output is as you expect: console.log(mathjs.floor(4.89 * 100) / 100) // 4.89
// or simply:
console.log(mathjs.floor(4.89, 2)) // 4.89 |
hello,math floor maybe wrong
case 1:
math.floor(math.bignumber("1234567890123456.9951").mul( math.bignumber(100)),0).toString()
is wrong , accept '123456789012345699' but '123456789012345700'
case2:
math.floor(math.bignumber("1234567890123456.9951").mul( math.bignumber(100)),1).toString()
is ok , value is : '123456789012345699.5'
The text was updated successfully, but these errors were encountered: