为什么 double 和 long double 在我的 64 位机器上完全相同?
这个问题听起来可能适合初学者,但是当我发现这个问题时,我认为我要么是初学者,要么我的电脑缺少一些东西:
int main()
{
cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;
cout << DBL_DIG << endl;
cout << LDBL_DIG << endl;
return 0;
}
程序输出:
8
8
15
15
我认为 long double
是 10 个字节,有 18 位十进制数字,而 double
是 8 个字节,有 15 位数字,但看来我错了。
为什么会这样呢?
在 64 位机器上使用 MSVC 2010。
This question may sound like for beginners, however when I found that out I thought I'm either a beginner or my comp is missing something:
int main()
{
cout << sizeof(double) << endl;
cout << sizeof(long double) << endl;
cout << DBL_DIG << endl;
cout << LDBL_DIG << endl;
return 0;
}
PROGRAM OUTPUT:
8
8
15
15
I thought long double
is 10 bytes and has 18 decimal digits while double
is 8 bytes and has 15 digits but it seems I was wrong.
Why is that so?
Using MSVC 2010 on 64bit machine.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
正如您所发现的,在 MSVC++ 中,
long double
是double
的同义词。显然这是为了利用仅限于 64 位操作的 SSE/SSE2/SSE3 指令集。另请参阅此处了解更多信息。
In MSVC++,
long double
is a synonym fordouble
as you've found out. Apparently this is to take advantage of SSE/SSE2/SSE3 instruction sets which are limited to 64-bit operations.See also here for more information.
所有基本类型的大小都是实现定义的,其中
最小值。特别是,您所能保证的就是
double
其精度和范围并不比
float
低,而且long
的精度和范围并不比double
double
低。从实现质量的角度来看,编译器应该给出
您是硬件提供的最好的。仅许多(大多数?)架构
有两种硬件支持的浮点类型;在这样的架构上,
double
和long double
通常是相同的。在一些架构中,让所有三个相同可能是有意义的。在
英特尔,质量实施将具有三种不同的类型,因为
这就是硬件提供的(但实现仍然是
即使所有三种浮点类型都相同)也符合要求)。在
另一方面,您可以为
long double
争论不同的大小:10(从未见过)、12 (g++) 或 16 字节,出于对齐原因(其中一些
未使用的字节)。 Intel 的实现,其中
long double
和double
是相同的,但是质量很差,而不是不合格。
The size of all of the basic types is implementation defined, with
minimums. In particular, all that you are guaranteed is that
double
doesn't have less precision and range than
float
, and thatlong
doesn't have less precision and range thandouble
double
.From a quality of implementation point of view, the compiler should give
you the best that the hardware offers. Many (most?) architectures only
have two hardware supported floating point types; on such architectures,
double
andlong double
will normally be identical. On somearchitectures, it might make sense to have all three identical. On
Intel, a quality implementation will have three different types, because
that's what the hardware offers (but an implementation would still be
compliant even if all three floating point types were identical). On
the other hand, you can argue different sizes for
long double
: 10(never seen), 12 (g++) or 16 bytes, for alignment reasons (with some of
the bytes unused). An implementation for Intel where
long double
anddouble
are identical is, however, simply poor quality, and notnon-conformant.