Can someone please explain the difference between the two, given the same radius? They produce different results. Thanks in advance
Code:#define PI 3.1416 volume = (4*PI/3)*pow(radius,3); volume = (4/3) * PI * pow(radius,3);
Can someone please explain the difference between the two, given the same radius? They produce different results. Thanks in advance
Code:#define PI 3.1416 volume = (4*PI/3)*pow(radius,3); volume = (4/3) * PI * pow(radius,3);
Hint: integer division.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
ahh okay thanks.
so if you do (4.0/3.0) it should work, right?
Yes. At least one of the constants needs to be floating point representation so that the compiler uses floating point math.