I was trying to find cube root of a perfect cube using pow function but the answers were unexpected (3375 = 15^3)
#include <bits/stdc++.h>
using namespace std;
int main()
{
double cb = pow(3375, 1.0/3.0);
printf("cb(in double) = %lf\n", cb);
printf("cb(in int) = %d\n",(int)cb);
return 0;
}
the output it shows is :
cb(in double) = 15.000000
cb(in int) = 14
After discussing it with people it turned out that the code ,
printf("%0.0lf", 14.0/3);
gives output
5
I am not getting why it's happening, and if it's due to precision in storing double and rounding off, then it should round it down to a smaller value rather than rounding to a greater value.