How to print/convert decimal floating point values in GCC?

倖福魔咒の 提交于 2021-02-07 14:22:20

问题


The GCC docs describe limited decimal floating point support in recent GCCs.

But how do I actually use it?

For example on Fedora 18, GCC 4.7.2.

A simple C program like

int main()
{
    _Decimal64 x = 0.10dd;
    return 0;
}

compiles (when using -std=gnu99) - but how do I actually do other useful stuff - like printing _Decimal64 values or converting strings to _Decimal64 values?

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?

I've tried

printf("%Df\n", x);

which did not work - printf just produced: %Df.


回答1:


As the docs say, GCC doesn't provide I/O, because printf etc. are provided by libc not by GCC.

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

A web search indicates Ubuntu packages this as libdfp, and it might be available on RHEL6 too.

The README says:

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.


来源:https://stackoverflow.com/questions/14924247/how-to-print-convert-decimal-floating-point-values-in-gcc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!