Previous: Formatted Output Functions, Up: Formatted Output   [Index]


10.3 C++ Formatted Output

The following functions are provided in libgmpxx (see Headers and Libraries), which is built if C++ support is enabled (see Build Options). Prototypes are available from <gmp.h>.

Function: ostream& operator<< (ostream& stream, const mpz_t op)

Print op to stream, using its ios formatting settings. ios::width is reset to 0 after output, the same as the standard ostream operator<< routines do.

In hex or octal, op is printed as a signed number, the same as for decimal. This is unlike the standard operator<< routines on int etc, which instead give twos complement.

Function: ostream& operator<< (ostream& stream, const mpq_t op)

Print op to stream, using its ios formatting settings. ios::width is reset to 0 after output, the same as the standard ostream operator<< routines do.

Output will be a fraction like ‘5/9’, or if the denominator is 1 then just a plain integer like ‘123’.

In hex or octal, op is printed as a signed value, the same as for decimal. If ios::showbase is set then a base indicator is shown on both the numerator and denominator (if the denominator is required).

Function: ostream& operator<< (ostream& stream, const mpf_t op)

Print op to stream, using its ios formatting settings. ios::width is reset to 0 after output, the same as the standard ostream operator<< routines do.

The decimal point follows the standard library float operator<<, which on recent systems means the std::locale imbued on stream.

Hex and octal are supported, unlike the standard operator<< on double. The mantissa will be in hex or octal, the exponent will be in decimal. For hex the exponent delimiter is an ‘@’. This is as per mpf_out_str.

ios::showbase is supported, and will put a base on the mantissa, for example hex ‘0x1.8’ or ‘0x0.8’, or octal ‘01.4’ or ‘00.4’. This last form is slightly strange, but at least differentiates itself from decimal.

These operators mean that GMP types can be printed in the usual C++ way, for example,

mpz_t  z;
int    n;
...
cout << "iteration " << n << " value " << z << "\n";

But note that ostream output (and istream input, see C++ Formatted Input) is the only overloading available for the GMP types and that for instance using + with an mpz_t will have unpredictable results. For classes with overloading, see C++ Class Interface.


Previous: Formatted Output Functions, Up: Formatted Output   [Index]