Make C floating point literals float (rather than double)

后端 未结 4 1080
走了就别回头了
走了就别回头了 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:53

    You can cast the defined constants to (float) wherever they are used, the optimizer should do its job. This is a portable solution.

    #define LIMIT 2.5
    
    if (x < (float)LIMIT) ...
    

提交回复
热议问题