Make C floating point literals float (rather than double)

后端 未结 4 1079
走了就别回头了
走了就别回头了 2020-12-04 17:40

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

4条回答
  •  春和景丽
    2020-12-04 17:46

    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.

提交回复
热议问题