[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
10.3 C++ Formatted Output
The following functions are provided in ‘libgmpxx’ (see section Headers and Libraries), which is built if C++ support is enabled (see section 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 standardostream 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 onint
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 standardostream 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 standardostream operator<<
routines do.The decimal point follows the standard library float
operator<<
, which on recent systems means thestd::locale
imbued on stream.Hex and octal are supported, unlike the standard
operator<<
ondouble
. The mantissa will be in hex or octal, the exponent will be in decimal. For hex the exponent delimiter is an ‘@’. This is as permpf_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 section 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.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.