有一个浮点字面后缀在C ++中使数字双精度? [英] Is there a floating point literal suffix in C++ to make a number double precision?
问题描述
我目前正在做一个C ++项目进行数值计算。广泛的绝大多数代码使用单精度浮点值,并且非常精确。因为这样我使用编译器标志来使基本浮点文字单精度而不是双精度,这是默认值。我发现这使表达式更容易阅读,我不必担心忘记一个'f'在某个地方。然而,每时每刻我需要双精度计算提供的额外的精度,我的问题是如何能够得到一个双精度字面量到这样的表达式。我试过的每一种方式首先将值存储在单个精度变量中,并将截断值转换为双精度值。
I'm currently working on a C++ project which does numerical calculations. The vast, vast majority of the code uses single precision floating point values and works perfectly fine with that. Because of this I use compiler flags to make basic floating point literals single precision instead of the double precision, which is the default. I find that this makes expressions easier to read and I don't have to worry about forgetting a 'f' somewhere. However, every now and then I need the extra precision offered by double precision calculations and my question is how I can get a double precision literal into such an expression. Every way I've tried so far first store the value in a single precision variable and the converts the truncated value to a double precision value. Not what I want.
到目前为止,我尝试的一些方法如下。
Some ways I've tried so far is given below.
#include <iostream>
int main()
{
std::cout << sizeof(1.0E200) << std::endl;
std::cout << 1.0E200 << std::endl;
std::cout << sizeof(1.0E200L) << std::endl;
std::cout << 1.0E200L << std::endl;
std::cout << sizeof(double(1.0E200)) << std::endl;
std::cout << double(1.0E200) << std::endl;
std::cout << sizeof(static_cast<double>(1.0E200)) << std::endl;
std::cout << static_cast<double>(1.0E200) << std::endl;
return 0;
}
使用单精度常数运行得到以下结果。
A run with single precision constants give the following results.
~/path$ g++ test.cpp -fsingle-precision-constant && ./a.out
test.cpp:6:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
test.cpp:7:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
test.cpp:12:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
test.cpp:13:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
test.cpp:15:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
test.cpp:16:3: warning: floating constant exceeds range of ‘float’ [-Woverflow]
4
inf
16
1e+200
8
inf
8
inf
我的理解是,最后两种情况提供的8个字节应该足够保持1.0E200,一个由以下输出支持的理论,其中相同的程序没有-fsingle-precision-constant。
It is my understanding that the 8 bytes provided by the last two cases should be enough to hold 1.0E200, a theory supported by the following output, where the same program is compiled without -fsingle-precision-constant.
~/path$ g++ test.cpp && ./a.out
8
1e+200
16
1e+200
8
1e+200
8
1e+200
上述示例建议的一种可能的解决方法是在四处使用四精度浮点数最初旨在使用双精度,并且当库需要时投射到双精度。
A possible workaround suggested by the above examples is to use quadruple precision floating point literals everywhere I originally intended to use double precision, and cast to double precision whenever required by libraries and such. However, this feels a bit wasteful.
推荐答案
Like Mark说,标准说它的双重除非它后面有一个f。
Like Mark said, the standard says that its a double unless its followed by an f.
标准背后有很好的理由,并使用编译器标志围绕它为了方便是坏习惯。
There are good reasons behind the standard and using compiler flags to get around it for convenience is bad practice.
因此,正确的方法是:
- 删除编译器标志
- 修复所有关于在浮点变量中存储双精度值时丢失精度的警告(添加所有f后缀)
- 省略f后缀。
它可能不是你正在寻找的答案,但它是你应该使用的方法,如果你关心你的代码的寿命基础。
Its probably not the answer you were looking for, but it is the approach you should use if you care about the longevity of your code base.
这篇关于有一个浮点字面后缀在C ++中使数字双精度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!