2013-02-17 83 views
4

gcc docs describe描述了最近GCC中的有限十进制浮点支持。如何在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

+1

http://sourceware.org/bugzilla/show_bug.cgi?id=11475似乎相关 – Mat 2013-02-17 18:14:10

回答

5

正如文档所说,GCC不提供I/O,因为printf等由libc而不是GCC提供。

IBM对GNU C库libdfp进行了扩展,它增加了printf钩子以使Decimal I/O工作。我没有用它,但你应该能够得到从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. 
+0

好吧,libdfp似乎是要走的路 - 不幸的是,似乎它不是通过Fedora存储库提供的 - 从源代码编译似乎并不那么简单 - 它抱怨“libbid中没有配置信息” - 另一方面,gcc编译的代码调用像__bid_muldd3这样的函数(当在_Decimal64值上执行操作) - 没有链接错误 - 因此,gcc似乎在运行时包含libbid。 – maxschlepzig 2013-02-17 21:49:15

+0

尝试使用配置选项'--with-backend = libdecnumber',它允许'configure'在Fedora 17上为我工作。'configure --help'表示这是默认设置,但显然它在说谎。虽然 – 2013-02-17 21:58:38

+0

后来编译失败,但似乎该库只能建立在IBM s390和powerpc系统上 – 2013-02-17 22:19:45

相关问题