Node:C++ Interface Rationals, Next:, Previous:C++ Interface Integers, Up:C++ Class Interface



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.

void mpq_class::mpq_class (type op) Function
void mpq_class::mpq_class (integer num, integer den) Function
Construct an mpq_class. The initial value can be a single value of any type, 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);

void mpq_class::mpq_class (mpq_t q) Function
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.

void mpq_class::mpq_class (const char *s) Function
void mpq_class::mpq_class (const char *s, int base) Function
void mpq_class::mpq_class (const string& s) Function
void mpq_class::mpq_class (const string& s, int base) Function
Construct an mpq_class converted from a string using mpq_set_str, (see Initializing Rationals). If the base is not given then 0 is used.

void mpq_class::canonicalize () Function
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.

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
int sgn (mpq_class op) Function
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.

mpz_class& mpq_class::get_num () Function
mpz_class& mpq_class::get_den () Function
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.

mpz_t mpq_class::get_num_mpz_t () Function
mpz_t mpq_class::get_den_mpz_t () Function
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.

istream& operator>> (istream& stream, mpq_class& rop); Function
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.