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.
Construct an
mpq_class. The initial value can be a single value of any type (conversion frommpf_classisexplicit), or a pair of integers (mpz_classor standard C++ integer types) representing a fraction, except thatlong longandlong doubleare not supported. For example,mpq_class q (99); mpq_class q (1.75); mpq_class q (1, 3);
Construct an
mpq_classfrom anmpq_t. The value in q is copied into the newmpq_class, there won't be any permanent association between it and q.
Construct an
mpq_classconverted from a string usingmpq_set_str(see Initializing Rationals).If the string is not a valid rational, an
std::invalid_argumentexception is thrown. The same applies tooperator=.
With C++11 compilers, integral rationals can be constructed with the syntax
123_mpqwhich is equivalent tompq_class(123_mpz). Other rationals can be built as-1_mpq/2or0xb_mpq/123456_mpz.
Put an
mpq_classinto canonical form, as per Rational Number Functions. All arithmetic operators require their operands in canonical form, and will return results in canonical form.
These functions provide a C++ class interface to the corresponding GMP C routines.
cmpcan be used with any of the classes or the standard C++ types, exceptlong longandlong double.
Get a reference to an
mpz_classwhich is the numerator or denominator of anmpq_class. This can be used both for read and write access. If the object returned is modified, it modifies the originalmpq_class.If direct manipulation might produce a non-canonical value, then
mpq_class::canonicalizemust be called before further operations.
Get a reference to the underlying
mpz_tnumerator or denominator of anmpq_class. This can be passed to C functions expecting anmpz_t. Any modifications made to thempz_twill modify the originalmpq_class.If direct manipulation might produce a non-canonical value, then
mpq_class::canonicalizemust be called before further operations.
Read rop from stream, using its
iosformatting settings, the same asmpq_t operator>>(see C++ Formatted Input).If the rop read might not be in canonical form then
mpq_class::canonicalizemust be called.