我们应该一般使用float字面量来代替更简单的双字面量吗? [英] Should we generally use float literals for floats instead of the simpler double literals?

查看:322
本文介绍了我们应该一般使用float字面量来代替更简单的双字面量吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C ++ (或者也许只有我们的编译器VC8和VC10) 3.14 是双字面值和 3.14f 是一个浮点文字。

In C++ (or maybe only our compilers VC8 and VC10) 3.14 is a double literal and 3.14f is a float literal.

现在我有一个同事说:


我们应该使用float-literals作为float计算,double-literals用于double计算,因为这可能会影响在计算中使用常量时计算的精度。

We should use float-literals for float calculations and double-literals for double calculations as this could have an impact on the precision of a calculation when constants are used in a calcualtion.

具体来说,我的意思是:

Specifically, I think he meant:

double d1, d2;
float f1, f2;
... init and stuff ...
f1 = 3.1415  * f2;
f1 = 3.1415f * f2; // any difference?
d1 = 3.1415  * d2;
d1 = 3.1415f * d2; // any difference?

或者由我添加,即使:

d1 = 42    * d2;
d1 = 42.0f * d2; // any difference?
d1 = 42.0  * d2; // any difference?

更一般地说,我只能看到 $ c> 2.71828183f 是确保我试图指定的常数将适合一个浮动(编译器错误/警告,否则)。

More generally, the only point I can see for using 2.71828183f is to make sure that the constant I'm trying to specify will actually fit into a float (compiler error/warning otherwise).

有人能说明一下吗?是否指定 f 后缀?为什么?

Can someone shed some light on this? Do you specify the f postfix? Why?

从一个答案引用我默认的理由:

To quote from an answer what I implicitly took for granted:


如果你使用一个float变量和一个双字面值,整个
操作将作为double操作,然后转换回float。

If you're working with a float variable and a double literal the whole operation will be done as double and then converted back to float.

这可能有什么伤害吗? (除了非常非常理论的性能影响吗?)

Could there possibly be any harm in this? (Other than a very, very theoretical performance impact?)

进一步编辑:如果包含技术细节的答案也可以包括这些差异如何影响通用代码 。 (是的,如果你是数字处理,你可能想确保你的big-n浮点运算是尽可能高效的(和正确的) - 但是对于被称为几次的通用代码很重要吗?Isn'如果代码只使用 0.0 并跳过 - 难以维护! - float suffix?)

Further edit: It would be nice if answers containing technical details (appreciated!) could also include how these differences affect general purpose code. (Yes, if you're number crunching, you probably like to make sure your big-n floating point ops are as efficient (and correct) as possible -- but does it matter for general purpose code that's called a few times? Isn't it cleaner if the code just uses 0.0 and skips the -- hard to maintain! -- float suffix?)

推荐答案

是的,应该使用 f 后缀。原因包括:

Yes, you should use the f suffix. Reasons include:


  1. 效能。当你写 float foo(float x){return x * 3.14; } ,您强制编译器发出将x转换为double的代码,然后执行乘法,然后将结果转换回单。如果添加 f 后缀,则会消除这两个转换。在许多平台上,每个转化都与乘法本身一样昂贵。

  1. Performance. When you write float foo(float x) { return x*3.14; }, you force the compiler to emit code that converts x to double, then does the multiplication, then converts the result back to single. If you add the f suffix, then both conversions are eliminated. On many platforms, each those conversions are about as expensive as the multiplication itself.

性能(续)。有一些平台(例如大多数手机),其上的双精度算法显着慢于单精度。即使忽略转换开销(覆盖在1.),每次强制计算以双重计算,你减慢你的程序。这不仅仅是一个理论问题。

Performance (continued). There are platforms (most cellphones, for example), on which double-precision arithmetic is dramatically slower than single-precision. Even ignoring the conversion overhead (covered in 1.), every time you force a computation to be evaluated in double, you slow your program down. This is not just a "theoretical" issue.

减少对错误的暴露。考虑示例 float x = 1.2; if(x == 1.2)// something; something 执行吗?不,它不是,因为x持有 1.2 四舍五入到 float ,但是正在与双精度value 1.2 。两者不相等。

Reduce your exposure to bugs. Consider the example float x = 1.2; if (x == 1.2) // something; Is something executed? No, it is not, because x holds 1.2 rounded to a float, but is being compared to the double-precision value 1.2. The two are not equal.

这篇关于我们应该一般使用float字面量来代替更简单的双字面量吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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