0

Has any one faced Math.js auto approximation issue and got any work around for this?

If I enter any number more than 18 digits then this library returns the approximate value; not the exact value. Lets say if user enters "03030130000309293689" then it returns "3030130000309293600" and when user enters "3030130000309293799" even it returns "3030130000309293600". Can we stop this approximation? This is a bug or if not then how can I avoid approximation?

Due to this approximation if any user enters "03030130000309293695 == 03030130000309293799" then it will always return true which is totally wrong.

github -- https://github.com/josdejong/mathjs

We can try this at http://mathjs.org/ ( in Demo notepad).

This is released for production!

I think if any time user enters like "03030130000309293695 == 03030130000309293799" both side number only then we can do string comparison. Rest all cases will be taken care by approximation.Why I am saying this is because if i use the same library for "73712347274723714284 *73712347274723713000" computation then it gives result in scientific notation.

Gaurav Pant
  • 4,029
  • 6
  • 31
  • 54
  • possible duplicate of [Javascript number gets another value](http://stackoverflow.com/questions/19533604/javascript-number-gets-another-value) – slebetman Oct 23 '13 at 12:30

3 Answers3

1

03030130000309293695 and 03030130000309293799 are pretty much the same number.

HOW?

According to this answer the limit of JS number is 9007199254740992(2^53). Your both numbers are greater than this number and so precision is left out. You probably need to use library like Big.js

It's not a library issue, it's just language architecture issue. You can even open your browser console and type in your equation to see it it truthy.

Community
  • 1
  • 1
lukas.pukenis
  • 13,057
  • 12
  • 47
  • 81
  • I think if any time user enters like "03030130000309293695 == 03030130000309293799" both side number only then we can do string comparison. Rest all cases will be taken care by approximation. – Gaurav Pant Oct 23 '13 at 11:54
  • @virus, yes, an approximation is compared as there's no way to represent this big number. – lukas.pukenis Oct 23 '13 at 12:58
0

This is not really a problem of Math.js but a result of how numbers work in javascript. Javascript uses 64bit binary floating point numbers (also known as 64bit double in C). As such, it has only 53 bits to store your number.

I've written an explanation here: Javascript number gets another value

You can read the wikipedia page for 64 bit doubles for more detail: http://en.wikipedia.org/wiki/Double_precision_floating-point_format

Now for the second part of your question:

If not then how can I avoid approximation?

There are several libraries in javascript that implements big numbers:

Community
  • 1
  • 1
slebetman
  • 109,858
  • 19
  • 140
  • 171
0

The latest version of math.js has support for bignumbers, see docs:

https://github.com/josdejong/mathjs/blob/master/docs/datatypes/bignumbers.md

Jos de Jong
  • 6,602
  • 3
  • 38
  • 58