Friday, March 29, 2019

c++ comparison of two double values not working properly




Look at this code:



#include 
#include
using namespace std;
class Sphere
{

double r;
public:
double V() const { return (4/3) * 3.14 * pow(r,3); }
bool equal(const Sphere& s) const
{
cout << V() << " == " << s.V() << " : " << ( V() == s.V() );
return ( V() == s.V() );

}


explicit Sphere(double rr = 1): r(rr){}

};
main()
{
Sphere s(3);
s.equal(s);
}



The output is 84.78 == 84.78 : 0 which means the same method doesn't return the same value every time, even though all parameters are static?



But if I write 3.0 instead of 3.14 in the V() method definition, like this:



double V() const { return (4/3) * 3.0 * pow(r,3); }


Then, the output is: 84.78 == 84.78 : 1



What is going on here? I need this method, for my program, which will compare volumes of two objects, but it is impossible? I banged my head for so long to figure out what is the cause of the problem and luckily I found it, but now I don't understand why?? Does it have something to do with the compiler (GCC) or am I missing something important here?



Answer



Comparing floating point values using the == operator is very error prone; two values that should be equal may not be due to arithmetic rounding errors. The common way to compare these is to use an epsilon:



bool double_equals(double a, double b, double epsilon = 0.001)
{
return std::abs(a - b) < epsilon;
}

No comments:

Post a Comment

plot explanation - Why did Peaches&#39; mom hang on the tree? - Movies &amp; TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...