为什么微软放弃长双精度数据类型?

发布于 2024-11-30 10:18:41 字数 232 浏览 1 评论 0原文

不久前,我编写了一个使用一些阶乘函数的程序。我使用长双精度数据类型来支持“相对”大数字。

现在,我从 codeblocks 更改为 Visualstudio 2010,我想知道为什么我的程序不再工作,直到经过一些研究后我意识到 MS 已经放弃了 long double 数据类型。 这有什么特殊原因吗?对我来说,这看起来就像是技术上的倒退。

有什么替代方案可以使用吗? (我也很乐意使用 boost 库中的替代方案)。

A while ago I wrote a program which used some factorial functions. I used the long double data type to support "relative" big numbers.

Now, I changed from codeblocks to Visualstudio 2010, I was wondering why my program didn't work any more till I realized after some research that MS has abandonded the long double data type.
Is there any special reason for this? To me it looks very like step backwards in terms of technology.

Is there any alternative to use? (I would also be happy with an alternative out of the boost library).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

凉薄对峙 2024-12-07 10:18:41

我不确定为什么您认为 long double 被“放弃”,因为它是 C++ 标准的一部分,因此兼容的实现必须实现它。

他们所做的“放弃”是long double 数学函数的重载,他们这样做是因为:

但是,在 Win32 编程中,long double 数据类型映射到 double 64 位精度数据类型。

反过来,旧 VS 版本中的 long double 为 80 位,是因为

FP 代码生成已改用 SSE/SSE2/SSE3 指令集,而不是 x87 FP 堆栈,因为这是 AMD 和 Intel 近代和未来芯片的性能重点。这些指令集仅支持 32 位和 64 位 FP 格式。

尽管如此,他们选择不支持这些重载,即使使用相同大小的 double 和 long double 类型(两者都可以做成 64 位),这是一个耻辱,因为它们也是 C++ 标准的一部分。但是,这就是适合您的 Microsoft。专心固执。

[n3290: 26.8]: 除了数学的 double 版本之外
中的函数,C++ 添加了 floatlong double 重载
这些函数的版本,具有相同的语义。

然而,尽管这些重载在 Visual Studio 中基本上已被弃用,但它们仍然可用,因此您仍然应该能够使用它们:

Microsoft 运行时库提供了long double 版本
数学函数仅用于向后兼容。


有什么替代方案可以使用吗? (我也很乐意使用 boost 库中的替代方案)。

在我看来,您一直依赖 long double 来支持特定范围的数值,因此当它在不同的工具链中发生变化时,会遇到回归问题。

如果您有特定的数字范围要求,请使用固定范围整数类型。这里有几个选项:

  • stdint.h - 一些 C++ 工具链支持作为扩展的 C99 功能;
  • stdint.h - Boost 作为库重新实现的 C99 功能;
  • cstdint - 如果您正在编写 C++0x 代码,则可能会用到 C++0x 功能。

I'm not sure why you think that long double was "abandoned", as it is part of the C++ Standard and therefore a compliant implementation must, well, implement it.

What they did "abandon" is long double overloads of mathematical functions, and they did this because:

In Win32 programming, however, the long double data type maps to the double, 64-bit precision data type.

which, in turn, along with long double in older VS versions being 80-bit, is because:

FP code generation has been switching to the use of SSE/SSE2/SSE3 instruction sets instead of the x87 FP stack since that is what both the AMD and Intel recent and future chip generations are focusing their performance efforts on. These instruction sets only support 32 and 64 bit FP formats.

Still, that they chose not to support these overloads, even with same-sized double and long double types (both could have been made 64-bit), is a shame because they are also part of the C++ Standard. But, well, that's Microsoft for you. Intently stubborn.

[n3290: 26.8]: In addition to the double versions of the math
functions in <cmath>, C++ adds float and long double overloaded
versions of these functions, with the same semantics.

However, although these overloads are essentially deprecated in Visual Studio, they are still available, so you should still be able to use them:

The Microsoft run-time library provides long double versions of the
math functions only for backward compatibility.


Is there any alternative to use? (I would also be happy with an alternative out of the boost library).

It sounds to me like you have been relying on long double to support a specific range of numeric values, and have consequently run into regression issues when that has changed in a different toolchain.

If you have a specific numeric range requirement, use fixed-range integral types. Here you have a few options:

  • stdint.h - a C99 feature that some C++ toolchains support as an extension;
  • stdint.h - a C99 feature that Boost re-implements as a library;
  • cstdint - a C++0x feature that may be of use if you are writing C++0x code.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文