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