如何在GCC中打印/转换十进制浮点值? [英] How to print/convert decimal floating point values in GCC?
问题描述
GCC文档描述了有限的十进制浮点支持在最近的海湾合作委员会中.
The GCC docs describe limited decimal floating point support in recent GCCs.
但是我该如何使用呢?
例如在Fedora 18,GCC 4.7.2上.
For example on Fedora 18, GCC 4.7.2.
一个简单的C程序,例如
A simple C program like
int main()
{
_Decimal64 x = 0.10dd;
return 0;
}
编译(使用-std = gnu99时)-但是我实际上如何做其他有用的事情-例如打印_Decimal64值或将字符串转换为_Decimal64值?
compiles (when using -std=gnu99) - but how do I actually do other useful stuff - like printing _Decimal64 values or converting strings to _Decimal64 values?
文档讨论了独立的C库实现",用于(我假设)诸如printf之类的东西-我必须使用哪个附加库-比如说-打印十进制浮点计算的结果?
The docs talk about 'a separate C library implementation' for (I assume) things like printf - which additional library do I have to use for - say - printing the result of a decimal floating point computation?
我尝试过
printf("%Df\n", x);
无效-printf刚产生:%Df
.
which did not work - printf just produced: %Df
.
推荐答案
正如文档所说,GCC不提供I/O,因为 printf
等是由libc提供的,而不是由GCC提供的.
As the docs say, GCC doesn't provide I/O, because printf
etc. are provided by libc not by GCC.
IBM对GNU C库libdfp进行了扩展,该库添加了 printf
钩子以使Decimal I/O起作用.我没有用过,但是您应该能够从eglibc svn存储库中获取代码并自己构建:
IBM contributed an extension to the GNU C library, libdfp, which adds printf
hooks to make Decimal I/O work. I haven't used it, but you should be able to get the code from the eglibc svn repository and build it yourself:
svn co http://www.eglibc.org/svn/libdfp/trunk libdfp
网络搜索表明Ubuntu将其打包为 libdfp
,并且它可能在RHEL6上也可用.
A web search indicates Ubuntu packages this as libdfp
, and it might be available on RHEL6 too.
自述文件说:
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中打印/转换十进制浮点值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!