Mathjs: math.chain(366.16).multiply(10).add(2).done()=3663.6000000000004

Created on 17 Oct 2017  路  6Comments  路  Source: josdejong/mathjs

I know js calculation is not accurate
But this is really a professional computing class library!!

professional! professional! professional!

Did not even solve!

invalid

Most helpful comment

snoopy83101, if you want to help, be nice. As it is your post is unhelpful and annoying. We are all volunteers here. How do you want to help?

All 6 comments

Use BigNumbers or Fractions instead of numbers

http://mathjs.org/docs/datatypes/numbers.html#roundoff-errors

It needs to be formatted again?!
"npm install mathjs"
"import math form 'mathjs'"
"math.chain().multiply().done() "
look ,Such a perfect code!
Now you tell me, Their results are all wrong by default!
You're just kidding me

I always thought mathjs was a perfect math class library!
JS this problem has existed for so many years
Lots and lots of scattered scripts can solve this
However, we let a default configuration output an error value

WTF take it easy man. Stop talking so aggressively. Take some time to study the issue in-depth before you start screaming.

This is the famous round-off error, which is always there when doing numerical calculations with irrational numbers. There is no free lunch here, it's all about making trade-offs. It's also not a JavaScript problem, it's a generic problem when trying to represent numbers in a computer.

  • Working with numbers is fast, and compatible with any other libraries that you may use. But it gives you these round-off errors.
  • Working with BigNumbers is slow, but gives you less round-off errors. Less. It doesn't solve the issue, but it is much less likely to occur. You're still working with a finite number of digits so you still can't represent 1/3 correctly.
  • Working with Fractions is great, but unfortunately you can't represent all irrational numbers as a Fraction.

In many use cases, working with numbers (having these round-off errors) is just fine: often performance and compatibility matters most, and the numbers may never be displayed on screen making round-off errors a non-issue, or you can easily round them to say 12 digits before displaying end-results to the user.

math.js allows you to simply configure which numeric solution suits you best, which I think is amazing. There are some ideas to make this even better and automatically convert non-BigNumber inputs to BigNumber inputs, see #864.

snoopy83101, if you want to help, be nice. As it is your post is unhelpful and annoying. We are all volunteers here. How do you want to help?

Maybe he can magically solve floating point precision limitations for everyone... maybe he can also invent us some gravity defying cars. Regardless there is no need for unhelpful arrogance.

Was this page helpful?
0 / 5 - 0 ratings