C++ 长双对双
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3454576/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
long double vs double
提问by CppLearner
I know that size of various data types can change depending on which system I am on. I use XP 32bits, and using the sizeof() operator in C++, it seems like long double is 12 bytes, and double is 8.
我知道各种数据类型的大小可能会根据我所在的系统而变化。我使用 XP 32 位,并在 C++ 中使用 sizeof() 运算符,似乎 long double 是 12 字节,double 是 8。
However, most major sources states that long double is 8 bytes, and the range is therefore the same as a double.
但是,大多数主要来源指出 long double 是 8 个字节,因此范围与 double 相同。
How come I have 12 bytes? If long double is indeed 12 bytes, doesn't this extends the range of value also? Or the long signature is only used (the compiler figures) when the value exceed the range of a double, and thus, extends beyond 8 bytes?
我怎么有 12 个字节?如果 long double 确实是 12 个字节,这是否也扩展了值的范围?或者只有当值超过双精度值的范围时才使用长签名(编译器数字),因此扩展到超过 8 个字节?
Thank you.
谢谢你。
采纳答案by Borealid
Quoting from Wikipedia:
引自维基百科:
On the x86 architecture, most compilers implement long double as the 80-bit extended precision type supported by that hardware (sometimes stored as 12 or 16 bytes to maintain data structure .
在 x86 架构上,大多数编译器将 long double 实现为该硬件支持的 80 位扩展精度类型(有时存储为 12 或 16 字节以维护数据结构 .
and
和
Compilers may also use long double for a 128-bit quadruple precision format, which is currently implemented in software.
编译器还可以将 long double 用于 128 位四倍精度格式,目前在软件中实现。
In other words, yes, a long double
maybe able to store a larger range of values than a double
. But it's completely up to the compiler.
换句话说,是的, along double
可能能够存储比 a 更大范围的值double
。但这完全取决于编译器。
回答by Shital Shah
For modern compilers on x64, Clang and GCC uses 16-byte double for long double
while VC++ uses 8-byte double. In other words, with Clang and GCC you get higher precision double but for VC++ long double
is same as double
. The modern x86 CPUs do support these 16-byte doubles so I think Clang and GCC are doing the right thing and allows you to access lower level hardware capability using higher level language primitives.
对于 x64 上的现代编译器,Clang 和 GCC 使用 16 字节双精度,long double
而 VC++ 使用 8 字节双精度。换句话说,使用 Clang 和 GCC 可以获得更高的精度 double 但对于 VC++long double
与double
. 现代 x86 CPU 确实支持这些 16 字节双精度,所以我认为 Clang 和 GCC 做的是正确的事情,并允许您使用高级语言原语访问低级硬件功能。
回答by scott77777
The standard byte sizes for numbers are the guaranteed minimum sizes across all platforms. They may be larger on some systems, but they will never be smaller.
数字的标准字节大小是所有平台上保证的最小大小。在某些系统上它们可能更大,但它们永远不会更小。
回答by Piotr Lenarczyk
As far as my programming newbie experience prompts:
据我的编程新手经验提示:
Use periodicaly normalized float [-1.0,+1.0]
Hold normalizing value separately with double, or long double
Normalizing introduces noise=small errors=high frequencies to variables values
From time to time it is useful to normalize with median value hold separately and keep data sorted (original data order could be saved as permutation vector)
使用定期标准化浮点数 [-1.0,+1.0]
用 double 或 long double 分别保存归一化值
归一化引入噪声=小错误=变量值的高频
有时,用单独的中值进行归一化并保持数据排序是有用的(原始数据顺序可以保存为置换向量)