07-26-2023, 04:28 PM
In C programming language, the floating point constant is double type by default
so `3.1415` is double type, unless use 'f' or 'F' suffix to indicate float type.
I assume `const float pi = 3.1415` will cause a warning, but actually not.
when I try these under gcc with -Wall:
float f = 3.1415926;
double d = 3.1415926;
printf("f: %f\n", f);
printf("d: %f\n", d);
f = 3.1415926f;
printf("f: %f\n", f);
int i = 3.1415926;
printf("i: %d\n", i);
the result is:
f: 3.141593
d: 3.141593
f: 3.141593
i: 3
the result (including double variable) obviously lose precision, but compile without any warning.
so what did the compiler do with this? or did I misunderstand something?
so `3.1415` is double type, unless use 'f' or 'F' suffix to indicate float type.
I assume `const float pi = 3.1415` will cause a warning, but actually not.
when I try these under gcc with -Wall:
float f = 3.1415926;
double d = 3.1415926;
printf("f: %f\n", f);
printf("d: %f\n", d);
f = 3.1415926f;
printf("f: %f\n", f);
int i = 3.1415926;
printf("i: %d\n", i);
the result is:
f: 3.141593
d: 3.141593
f: 3.141593
i: 3
the result (including double variable) obviously lose precision, but compile without any warning.
so what did the compiler do with this? or did I misunderstand something?