You have specified that 20 digits after the decimal point should be printed. And so they are. But that does not mean that these digits are correct and meaningful.
You have also noticed that the last four digits are zeroes. That is because a
double
has that precision and the additional digits are set to zero by the
printf()
function.
The reason that both printed values have these 16 digits is sourced by the fact that the
printf()
function only supports
double
values as floating point arguments and passed
float
arguments are converted to
double
behind the scenes. The compiler is processing it as:
printf("float = %.20f \n", (double)f);
The resulting values printed with appropriate resolution are:
input = 3.1415926535897932384626433832795
double = 3.14159265358979311
float = 3.141592741
As you can see the
float
differs from the input at the 8th digit and the
double
at the 17th digit.