Below is an excerpt from a program -
float val=214.20;
double val1=214.20;
printf("\n\n\nfloat :: %f, %4.6f, %4.2f \n ",val,val,val);
printf("double:: %f, %4.6f, %4.2f \n ",val1,val1,val1);
And the output is -
float :: 214.199997, 214.199997, 214.20<--- is the correct value as we wanted
double:: 214.200000, 214.200000, 214.20
I understand that 214.20 has infinite binary representation. The first two elements of first line has approximation of the intended value, but the last value has absolutely same data. This caused a question in my mind -
How the scanf/fscanf/sprintf functions treat the precision formats ??
With no precision provided it printed an approximate value, but with %4.2f
it gave correct result. Can some friend may explain me the algorithm used by scanf when handling precisions.
Thanks, Ripunjay