### 12.3 C++ Interface Rationals

In all the following constructors, if a fraction is given then it should be in canonical form, or if not then `mpq_class::canonicalize` called.

Function: mpq_class::mpq_class (type op)
Function: mpq_class::mpq_class (integer num, integer den)

  Construct an `mpq_class`. The initial value can be a single value of any type (conversion from `mpf_class` is `explicit`), or a pair of integers (`mpz_class` or standard C++ integer types) representing a fraction, except that `long long` and `long double` are not supported. For example,

      mpq_class q (99);
      mpq_class q (1.75);
      mpq_class q (1, 3);

Function: explicit mpq_class::mpq_class (const mpq_t q)

  Construct an `mpq_class` from an `mpq_t`. The value in q is copied into the new `mpq_class`, there won’t be any permanent association between it and q.

Function: explicit mpq_class::mpq_class (const char *s, int base = 0)
Function: explicit mpq_class::mpq_class (const string& s, int base = 0)

  Construct an `mpq_class` converted from a string using `mpq_set_str` (see Initializing Rationals).

  If the string is not a valid rational, an `std::invalid_argument` exception is thrown. The same applies to `operator=`.

Function: mpq_class operator"" _mpq (const char *str)

  With C++11 compilers, integral rationals can be constructed with the syntax `123_mpq` which is equivalent to `mpq_class(123_mpz)`. Other rationals can be built as `-1_mpq/2` or `0xb_mpq/123456_mpz`.

Function: void mpq_class::canonicalize ()

  Put an `mpq_class` into canonical form, as per Rational Number Functions. All arithmetic operators require their operands in canonical form, and will return results in canonical form.

Function: mpq_class abs (mpq_class op)
Function: int cmp (mpq_class op1, type op2)
Function: int cmp (type op1, mpq_class op2)
Function: double mpq_class::get_d (void)
Function: string mpq_class::get_str (int base = 10)
Function: int mpq_class::set_str (const char *str, int base)
Function: int mpq_class::set_str (const string& str, int base)
Function: int sgn (mpq_class op)
Function: void mpq_class::swap (mpq_class& op)
Function: void swap (mpq_class& op1, mpq_class& op2)

  These functions provide a C++ class interface to the corresponding GMP C routines.

  `cmp` can be used with any of the classes or the standard C++ types, except `long long` and `long double`.

Function: mpz_class& mpq_class::get_num ()
Function: mpz_class& mpq_class::get_den ()

  Get a reference to an `mpz_class` which is the numerator or denominator of an `mpq_class`. This can be used both for read and write access. If the object returned is modified, it modifies the original `mpq_class`.

  If direct manipulation might produce a non-canonical value, then `mpq_class::canonicalize` must be called before further operations.

Function: mpz_t mpq_class::get_num_mpz_t ()
Function: mpz_t mpq_class::get_den_mpz_t ()

  Get a reference to the underlying `mpz_t` numerator or denominator of an `mpq_class`. This can be passed to C functions expecting an `mpz_t`. Any modifications made to the `mpz_t` will modify the original `mpq_class`.

  If direct manipulation might produce a non-canonical value, then `mpq_class::canonicalize` must be called before further operations.

Function: istream& operator>> (istream& stream, mpq_class& rop);

  Read rop from stream, using its `ios` formatting settings, the same as `mpq_t operator>>` (see C++ Formatted Input).

  If the rop read might not be in canonical form then `mpq_class::canonicalize` must be called.