Sorry if this has been asked before but I was very unsure how to formulate my search to get any relevant results.
Basically I have a class "Wheel", and within that class I am declaring how the == operator should work:
bool operator == (Wheel& anotherWheel){
//Circumference of a wheel, times its RPM should give its speed
//The speed will be in what ever unit the radius is, per minute
double ourSpeed = 2 * PI * radius * rpm;
double anotherSpeed = 2 * PI * anotherWheel.getRadius() * anotherWheel.getRpm();
cout << ourSpeed << " vs " << anotherSpeed << endl;
if (ourSpeed == anotherSpeed){
return true;
}
return false;
This works except for when the radius and RPM of a wheel are the same as the others, except switched. So in other words it does not return true for:
2*PI*3*10 vs 2*PI*10*3
Even though I print it out and they are the exact same in the console (as they should be unless my basic math knowledge is completely out of whack).
I managed to solve it by adding a paranthesis when calculating the speeds:
double ourSpeed = 2 * PI * (radius * rpm);
double anotherSpeed = 2 * PI * (anotherWheel.getRadius() * anotherWheel.getRpm());
But I want to understand why this happens, PI is just a constant double I declared so that shouldn't matter.
Thanks!