11.3 C++ Formatted Input

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

Function: istream& operator>> (istream& stream, mpz_t rop)

Read rop from stream, using its ios formatting settings.

Function: istream& operator>> (istream& stream, mpq_t rop)

An integer like ‘123’ will be read, or a fraction like ‘5/9’. No whitespace is allowed around the ‘/’. If the fraction is not in canonical form then mpq_canonicalize must be called (see Rational Number Functions) before operating on it.

As per integer input, an ‘0’ or ‘0x’ base indicator is read when none of ios::dec, ios::oct or ios::hex are set. This is done separately for numerator and denominator, so that for instance ‘0x10/11’ is 16/11 and ‘0x10/0x11’ is 16/17.

Function: istream& operator>> (istream& stream, mpf_t rop)

Read rop from stream, using its ios formatting settings.

Hex or octal floats are not supported, but might be in the future, or perhaps it’s best to accept only what the standard float operator>> does.

Note that digit grouping specified by the istream locale is currently not accepted. Perhaps this will change in the future.


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

mpz_t  z;
...
cin >> z;

But note that istream input (and ostream output, see C++ Formatted Output) 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.