www.delorie.com/gnu/docs/gmp/gmp_67.html   search  
 
Buy GNU books!


GNU MP 4.1.2

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.3 C++ Formatted Output

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

Function: ostream& operator<< (ostream& stream, 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, 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, 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 current locale, on systems providing localeconv.

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 section 11.3 C++ Formatted Input) is the only overloading available and using for instance + with an mpz_t will have unpredictable results.


[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

  webmaster   donations   bookstore     delorie software   privacy  
  Copyright © 2003   by The Free Software Foundation     Updated Jun 2003