I want to understand how C handles the loss of precision for floating point number.
Here's my simple code:
#include <stdio.h>
#include <math.h>
int main ()
{
double a;
int i;
i = 7;
a = sqrt(i);
printf("i = %d, a = %lf\n", i, a);
printf("a * a = %lf\n", a*a);
a = 2.645751;
printf("a * a = %lf\n", a*a);
return(0);
}
Following is the result after cc
i = 7, a = 2.645751
a * a = 7.000000
a * a = 6.999998
If directly assigned a floating number which is 2.645751, the result of a * a looks understandable to me.
But if a is assigned sqrt(7), why there is no loss of precision for the output of a * a?
That is hard for me to understand.