a float decimal value such as 0.1 is converted by the compiler into a binary equivalent using some float point representation.
Typically a float represented by 32 bit binary has a precision of about 7 significant figures.
Copnsider the following version of your program
it is the precision of all the digits not just after the decimal point.
Consider pi = 3.1415926535897932384626433 - if float was 6 digits only 3.14159 would be significant.
in header file <float.h> there are constants that specify the precisions and other information for float types, e.g. FLT_DIG and DBL_DIG cfloat (float.h) - C++ Reference
consider the following program
Code:
#include <stdio.h>
#include <float.h>
int main()
{
float pi=3.1415926535897932384626433f;
double pid = 3.1415926535897932384626433;
printf("FLT_DIG %d DBL_DIG %d \n", FLT_DIG, DBL_DIG );
printf("float pi %2.20f\n", pi);
printf("double pi %2.20f\n", pid);
}
when run using gcc on a Windows PCgives
Code:
FLT_DIG 6 DBL_DIG 15
float pi 3.141592741012573
double pi 3.141592653589793
you can see although the float pi is printed to 20 places after the decimal point only the first 5 or 6 digits are significant, the rest are rubbish.
the double pi is significant to about 15 figures.