It is well known that in C, floating point literals (e.g. 1.23
) have type double
. As a consequence, any calculation that involves them is promoted
Use warnings instead: -Wdouble-promotion
warns about implicit float to double promotion, as in your example. -Wfloat-conversion
will warn about cases where you may still be assigning doubles to floats.
This is a better solution than simply forcing double values to the nearest float value. Your floating-point code is still compliant, and you won't get any nasty surprises if a double value holds a positive value, say, less than FLT_DENORM_MIN
(assuming IEEE-754) or greater than FLT_MAX
.