使C浮点文字浮动(而非双) [英] Make C floating point literals float (rather than double)

查看:254
本文介绍了使C浮点文字浮动(而非双)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,在C,浮点文字(例如 1.23 )的类型为双击。因此,涉及它们被提升到一倍。任何计算

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 to double.

我的工作是有只支持单precision(浮动)的数字浮点单元的嵌入式实时系统。我所有的变量是浮动,这precision就足够了。我不需要(也不能够负担得起)双击可言。但每一次类似

I'm working on an embedded real-time system that has a floating point unit that supports only single precision (float) numbers. All my variables are float, and this precision is sufficient. I don't need (nor can afford) double at all. But every time something like

if (x < 2.5) ...

写的是灾难发生:减速可以达到两个数量级。当然,直接的答案是写

is written, disaster happens: the slowdown can be up to two orders of magnitude. Of course, the direct answer is to write

if (x < 2.5f) ...

不过这是很容易错过(和难以察觉,直到为时已晚),特别是当'配置'值的#define 倒是由一个单独的文件一个不太纪律(或只是新)开发商。

but this is so easy to miss (and difficult to detect until too late), especially when a 'configuration' value is #define'd in a separate file by a less disciplined (or just new) developer.

那么,有没有办法迫使编译器把所有的(浮点)面值为float,仿佛用后缀˚F?即使它是对的规格,我不在乎。或任何其他解决方案?编译器是gcc,顺便说一句。

So, is there a way to force the compiler to treat all (floating point) literals as float, as if with suffix f? Even if it's against the specs, I don't care. Or any other solutions? The compiler is gcc, by the way.

推荐答案

-fsingle- precision -constant 标志可以使用。它会导致在单个precision加载浮点常量,即使这并不确切。

-fsingle-precision-constant flag can be used. It causes floating-point constants to be loaded in single precision even when this is not exact.

注意 - 这也将使用双precision变量的操作单precision常量。

Note- This will also use single precision constants in operations on double precision variables.

这篇关于使C浮点文字浮动(而非双)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆