The Decimal TR defines overloads for input and output in sections 3.2.10 and 3.2.11, respectively. From the looks of it they are not implemented by gcc. The decimal support in gcc is implemented in terms of libdecnum which provides routines to convert from and to strings. I would create a simple implementation of the I/O operators using them to get started.
Edit on 2012-10-17: Looking at formatting the decimal values in gcc's library (std::decimal::decimal64
and family) revealed that gcc unfortunately doesn't install either the decNumber library headers or the library. Furthermore, other sources of decNumber don't quite align with the version shipping with gcc. As a result, the only way I found so far to get decimal numbers formatted is to use explicit declarations and using the library build during compiling gcc.
The simple part is the source, although for a rather simple implementation. First, the declaration which would go into a suitable header, e.g. <decimal/decimal>
:
std::ostream& operator<< (std::ostream&, std::decimal::decimal32 const&);
std::ostream& operator<< (std::ostream&, std::decimal::decimal64 const&);
std::ostream& operator<< (std::ostream&, std::decimal::decimal128 const&);
A fairly simple implementation of something doing some formatting, although without any options could look like this:
extern "C" char* decimal32ToString(std::decimal::decimal32 const*, char*);
extern "C" char* decimal64ToString(std::decimal::decimal64 const*, char*);
extern "C" char* decimal128ToString(std::decimal::decimal128 const*, char*);
std::ostream& operator<< (std::ostream& out,
std::decimal::decimal32 const& value)
{
char buffer[128];
decimal32ToString(&value, buffer);
return out << buffer;
}
std::ostream& operator<< (std::ostream& out,
std::decimal::decimal64 const& value)
{
char buffer[128];
decimal64ToString(&value, buffer);
return out << buffer;
}
std::ostream& operator<< (std::ostream& out,
std::decimal::decimal128 const& value)
{
char buffer[128];
decimal128ToString(&value, buffer);
return out << buffer;
}
With this in place, there is still an issue that I didn't manage to build anything using decimals unless some level of optimization, i.e., -O1
(or higher), is used. There are unreferenced symbols unless optimization is used but this is entirely unrelated to printing the values. To get definitions of the functions used in the implementation, I needed to link to the libdecnumber.a
library which is created while gcc is being build:
g++ -o prog decimal-io.cpp main.cpp <gcc-build-root>/libdecnumber/libdecnumber.a
Aside from this I have my own implementation of the Decimal TR with some extension and basing it on the decnumber library and implement I/O properly. My implementation hopefully will become publicly available at some point but not any time soon.