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

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

But how do I really 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 things - for example, printing _Decimal64 values ​​or converting strings to _Decimal64 values?

The docs talk about a “separate C library implementation” for (I guess) things like printf — what additional library should I use, for example, to print the result of decimal floating point calculations?

I tried

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

which did not work - only printf: %Df printed.

+4
source share
1 answer

As the docs say, GCC does not provide I / O because printf , etc. provided by libc not gcc.

IBM has provided an extension for the GNU C library, libdfp, which adds printf hooks for decimal I / O. I have not used it, but you can get the code from the eglibc svn repository and create it yourself:

 svn co http://www.eglibc.org/svn/libdfp/trunk libdfp 

A web search shows that Ubuntu packages are like libdfp , and it can also be accessed on RHEL6.

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. 
+5
source

All Articles