1

Is there a solution to the infamous floating point math error in JavaScript due to the 64-bit floating point representation? Is floating point math broken?

Im trying to make a math intensive application based on JavaScript, however, due to this error in JavaScript, it always results in inaccurate output.

For ex.: 0.1 + 0.2 = 0.30000000000000004 whereas the expected is 0.1 + 0.2 = 0.3

I wonder how the financial organizations like (PayPal) are still up with there javascript applications.

Community
  • 1
  • 1
Temp O'rary
  • 5,366
  • 13
  • 49
  • 109
  • This is a common issue with not only javascript but quite a lot of languages, the issue is in binary > nonbinary conversion and is not solvable on "our end" without a cutoff, you have to do a cutoff. – Dellirium Dec 07 '16 at 12:35
  • 6
    This isn't an error so much as an inherent limitation of floating-point arithmetic. Floating-point values only *approximate* real numbers. – chepner Dec 07 '16 at 12:35
  • @chepner, I know of the above question ("Is floating point math broken? ") and hence I've quoted it myself. If you see that question you will find that it asks and I quote "Any ideas why this happens?", whereas, my question begins with "Is there a solution to...". Hence, this should be considered not duplicate. I want to discuss a solution to that problem. – Temp O'rary Dec 10 '16 at 17:24
  • The accepted answer to that question links to discussions of how to properly use floating-point values. There is no "fix", because floating-point arithmetic is not broken. – chepner Dec 10 '16 at 17:41

2 Answers2

6

The "solution" to the (non-)error is to ignore it, and ensure that you use an appropriate function, i.e. toFixed(n), to present the number with the desired number of decimal places.

It's important to know the difference between the internal representation of a value, and how to present that value to the end user.

Alnitak
  • 334,560
  • 70
  • 407
  • 495
  • The problem is when my application is Math intensive like a scientific calculations/accounting/banking/financial application. The app will have to be as accurate as possible and match at least the values shown on a calculator. Hence this question is relevant. – Temp O'rary Dec 12 '16 at 09:28
  • @TempO'rary on the average, the rounding "errors" will even out. The typical errors using double precision floating point are comparable to the width of a human hair compared to the Earth's orbit around the sun. For the vast majority of problems that's utterly insignificant. To get "calculator precision", just make sure you don't present any more than about 14 significant figures. – Alnitak Dec 12 '16 at 12:03
  • 1
    oh, and for accounting purposes, don't use FP at all, use a "decimal" class, or make your "unit" value the smallest currency amount and use integers, e.g. represent 1 dollar as "100" – Alnitak Dec 12 '16 at 12:04
  • but let me know how to work with this problem: x = 0.2; y = 0.3; z = 0.1; console.log(x - y + z) – Temp O'rary Dec 12 '16 at 16:23
  • 1
    @TempO'rary `console.log((x - y + z).toFixed(14))` – Alnitak Dec 12 '16 at 17:35
2

You can use https://github.com/ekg/fraction.js library for the intermediate values and convert the final result back to decimal value to minimize error.

May Rest in Peace
  • 2,070
  • 2
  • 20
  • 34