|
|
|
|
|
by tedchs
4940 days ago
|
|
I should point out this is normal for floating-point numbers. Erlang and Ruby do the exact same thing: $ erl
1> 0.2 + 0.1.
0.30000000000000004
2> 0.94 - 0.01.
0.9299999999999999
3> 0.3 + 0.544.
0.8440000000000001
$ irb
1.9.2-p290 :001 > 0.2 + 0.1
=> 0.30000000000000004
1.9.2-p290 :002 > 0.94 - 0.01
=> 0.9299999999999999
1.9.2-p290 :003 > 0.3 + 0.544
=> 0.8440000000000001
1.9.2-p290 :004 >
|
|
You use something like:
where epsilon is usually defined by the language as a very small quantity (example values in C are 1E-8 for a float, 1E-15 for double), or you can just use a hard coded value appropriate to the values you are comparing.