1 |
On 08/22/2015 09:42 AM, R0b0t1 wrote: |
2 |
> https://en.wikipedia.org/wiki/Round-off_error |
3 |
> https://en.wikipedia.org/wiki/Machine_epsilon |
4 |
> |
5 |
> Either add a tolerance (a - b <= t) or compare them as strings as |
6 |
> you've been doing. |
7 |
> |
8 |
|
9 |
You probably want |a - b| <= t there =) |
10 |
|
11 |
But... that can cause problems too. If your numbers are small enough, |
12 |
you can wind up with infinity or NaN (not a number) and then your |
13 |
comparisons will go berserk. |
14 |
|
15 |
Floating point addition isn't even commutative: |
16 |
|
17 |
> 0.1 + 0.2 + 0.3 |
18 |
0.6000000000000001 |
19 |
> 0.1 + (0.2 + 0.3) |
20 |
0.6 |
21 |
|
22 |
Better to avoid that quagmire entirely if you can. Use fixed point |
23 |
arithmetic, arbitrary precision, or even rational numbers if you can get |
24 |
away with it. |