Reputation: 6343
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!
Upvotes: 1
Views: 121
Reputation: 4810
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.
Upvotes: 1
Reputation: 1361
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.
Upvotes: 0