I am trying to divide an integer by a double value but i believe its losing precision..
#include <cmath>
#include <cstdio>
int main()
{
double t=5465/54.0;
double t1=(double)5465/(double)(t);
double t3 = 5465.0/101.203;
printf("%lf %lf %lf\n",t,t1,t3);
return 0;
}
For the above code value of t3 = 54.0003 is as expected but for t1 it becomes 54 instead of getting same value as t3.
I can’t get what mistake am i doing
The value of 5465/54.0 is 101.203703704 (to 9 decimal places). In your code you are using this
truncating the result of
to
101.203for some unknown reason. Therefore you are calculating 2 different values as a result of the truncation. Essentially you are expecting these 2 calculations to be the same but