floating point - Assign double constant to float variable without warning in C? -
in c programming language, floating point constant double type default
3.1415
double type, unless use 'f' or 'f' suffix indicate float type.
i assume const float pi = 3.1415
cause warning, not.
when try these under gcc -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 = 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) lose precision, compile without warning.
did compiler this? or did misunderstand something?
-wall
not enable warnings loss of precision, truncation of values, etc. because these warnings annoying noise , "fixing" them requires cluttering correct code heaps of ugly casts. if want warnings of nature need enable them explicitly.
also, use of printf
has nothing precision of actual variables, precision printf
printing at, defaults 6 places after decimal point.
Comments
Post a Comment