GCC 文档描述了 limited decimal floating point support在最近的 GCC 中。
但我如何实际使用它?
例如在 Fedora 18、GCC 4.7.2 上。
一个简单的 C 程序,例如
int main()
{
_Decimal64 x = 0.10dd;
return 0;
}
编译(使用 -std=gnu99 时) - 但我实际上如何做其他有用的事情 - 比如打印 _Decimal64 值或将字符串转换为 _Decimal64 值?
文档讨论了(我假设)诸如 printf 之类的“单独的 C 库实现”-我必须使用哪个附加库来打印十进制浮点计算的结果?
我试过了
printf("%Df\n", x);
这不起作用 - printf 刚刚生成:
%Df
.
最佳答案
正如文档所说,GCC 不提供 I/O,因为 printf
等由 libc 而不是 GCC 提供。
IBM 为 GNU C 库 libdfp 提供了一个扩展,它添加了 printf
使 Decimal I/O 工作的钩子(Hook)。我没用过,但你应该可以从 eglibc svn 存储库中获取代码并自己构建它:
svn co http://www.eglibc.org/svn/libdfp/trunk libdfp
网络搜索表明 Ubuntu 将其打包为
libdfp
,它也可能在 RHEL6 上可用。自述文件说:
When libdfp is loaded printf will recognize the following length modifiers:
%H - for _Decimal32
%D - for _Decimal64
%DD - for _Decimal128
It will recognize the following conversion specifier 'spec' characters:
e,E
f,F
g,G
a,A (as debuted in ISO/IEC TR 24732)
Therefore, any combination of DFP length modifiers and spec characters is
supported.
关于gcc - 如何在 GCC 中打印/转换十进制浮点值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14924247/