微软为何放弃 long double 数据类型?
前段时间我写了一个程序,它使用了一些阶乘函数.我使用 long double 数据类型来支持相对"大数字.
现在,我从代码块更改为 Visualstudio 2010,我想知道为什么我的程序不再工作,直到经过一些研究后我意识到 MS 已经放弃了 long double 数据类型.这有什么特别的原因吗?对我来说,这看起来像是技术方面的倒退.
有什么替代品吗?(我也会对 boost 库中的替代方案感到满意.
解决方案我不知道你为什么认为 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++ 标准的一部分.但是,好吧,这就是你的微软.很固执.
[n3290: 26.8]:
除了数学的 double
版本<cmath>
中的函数,C++ 增加了 float
和 long double
重载这些函数的版本,具有相同的语义.
然而,尽管这些重载在 Visual Studio 中基本上已被弃用,但它们仍然可用,因此您应该仍然可以使用它们:
<块引用>Microsoft 运行时库提供 long double
版本的数学函数仅用于向后兼容.
<小时><块引用>
有什么替代品吗?(我也会对 boost 库中的替代方案感到满意.
在我看来,您一直在依赖 long double
来支持特定范围的数值,因此在不同的工具链中发生了变化时遇到了回归问题.
如果您有特定的数值范围要求,请使用固定范围整数类型.在这里你有几个选择:
stdint.h
- 一些 C++ 工具链作为扩展支持的 C99 功能;stdint.h
- Boost 重新实现为库的 C99 功能;cstdint
- 如果您正在编写 C++0x 代码,可能会用到一个 C++0x 功能.
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).
解决方案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 thedouble
, 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 thedouble
versions of the math functions in<cmath>
, C++ addsfloat
andlong 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.
相关文章