Next: C++ Interface Floats, Previous: C++ Interface Integers, Up: C++ Class Interface [Index]
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 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);
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.
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=
.
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
.
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.
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
.
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.
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.
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.
Next: C++ Interface Floats, Previous: C++ Interface Integers, Up: C++ Class Interface [Index]