1

In my application, I am doing some client-side math, and it often comes out wrong due to floating point errors. To simplify the problem:

Math.floor((.3 - .1) * 10) == 1

There are other more complicated examples, but my question is this:

What is the appropriate way to solve for potential floating point errors? Example

Math.floor(fixFloat(.3 - .1) * 10) == 2

Thanks!

wizulus
  • 5,653
  • 2
  • 23
  • 40
  • 1
    Did you see this http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – Manu Feb 27 '14 at 17:56
  • Ah thank you.. But for my purposes, the accepted answer there is too cumbersome. The best answer in my opinion is `.toPrecision(12)` – wizulus Feb 27 '14 at 18:15
  • in questions like these(ones that have large number of upvotes), its always a good idea to go through the top 3 answers, and choose the one that suits you.. :) – Manu Feb 27 '14 at 18:19
  • This may be useful to you http://stackoverflow.com/questions/1458633/elegant-workaround-for-javascript-floating-point-number-problem – user2118784 Feb 27 '14 at 18:34

2 Answers2

1

This is not a JavaScript question but rather a floating point error analysis question. Luckily, this is a pretty well understood problem.

This page describes in detail how to analyze floating point errors. This will let you assign error bounds to your computations. Other good reading is the wikipedia page on the IEEE floating point format.

You cannot actually prevent such errors. However, you can provide tight bounds on them so that users/clients can have confidence in the value produced. Additionally, in some cases you can reformat the equation to reduce the error bounds.

If you have more questions about floating point error analysis, it'd be best to ask over on http://math.stackexchange.com as this is really a mathematical area.

J David Smith
  • 4,780
  • 1
  • 19
  • 24
0

Floating point is in its very nature not for "exact" computations, if you want precision use fixed point math, e.g. store money in cents/pennies.

Moritz Mahringer
  • 1,240
  • 16
  • 28