comparison - Comparing two doubles in c++ not yielding correct result -
i have 2 doubles, , b, in c++ should equal reason not being treated such. following code:
cout << << "==" << b << ": " << (a == b) << endl;
is outputting
0.5 == 0.5: 0
any ideas why resolving false?
to avoid errors when comparing decimals might useful use function:
bool aresame(double a, double b) { return fabs(a - b) < epsilon; }
where epsilon represented small number such .001
grabbed this question.
edit: including <limits>
1 use std::numeric_limits<double>::epsilon()
Comments
Post a Comment