Neither 11.9 nor 108.9 can be represented exactly in a float
. You will get better accuracy if you use double
.
If you change the precision of printing, you will see how accurately those are represented.
#include <stdio.h>
int main()
{
int a = 8; float b = 3.9; char c='a';
float hasil = a+b;
printf("%.16f\n", hasil);
hasil = a+b+c;
printf("%.16f\n", hasil);
return 0;
}
Output:
11.8999996185302734
108.9000015258789062
By changing the type to double
, I get more accurate output:
#include <stdio.h>
int main()
{
int a = 8; double b = 3.9; char c='a';
double hasil = a+b;
printf("%.16f\n", hasil);
hasil = a+b+c;
printf("%.16f\n", hasil);
return 0;
}
Output:
11.9000000000000004
108.9000000000000057
Those results are from gcc running on a Linux desktop. I suspect you will get similar, if not exactly same, results in other common compilers too.