Re: Floating point computations differ in different runs of same program
Tim Slattery wrote:
Richard Herring <junk@[127.0.0.1]> wrote:
In message
<7775ee7d-0bc9-4a3c-b3c9-0b015a8b22df@d70g2000hsf.googlegroups.com>,
kasthurirangan.balaji@gmail.com writes
Do not use "=="
operator for comparing doubles.
Do not offer such sweeping generalisations.
If it weren't appropriate to use operator== on doubles, the language
wouldn't supply it.
Since floating point numbers - single or double precision - are
approximations, the == operator is not appropriate for them.
I do not understand what relevance there is between the fact that
FP numbers are approximations and the appropriateness of equality
operator for them. Could you please elaborate? Also, consider
that if I write
double a = 0.1; // it's most likely an approximation
double b = a;
assert(a == b);
the assertion is true, approximation or not.
Best way is using abs, subtraction and
less than operator with the precisions defined.
The "best" way depends on what you are trying to do. If testing for
equality is what you want to do, then operator== is the best way.
fabs(d1-d2) < 0.0001
where d1,d2 are two doubles.
This may be appropriate in some circumstances, but it is not a test
for equality.
Maybe, but it's the best you can do with floating point numbers.
Just as well as you can do any other test. Like < or >. Just don't
call it "equality". That's all Richard is saying. And why "maybe"?
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask