[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
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 frommpf_class
isexplicit
), or a pair of integers (mpz_class
or standard C++ integer types) representing a fraction, except thatlong long
andlong 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 anmpq_t
. The value in q is copied into the newmpq_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 usingmpq_set_str
(see section Initialization and Assignment Functions).If the string is not a valid rational, an
std::invalid_argument
exception is thrown. The same applies tooperator=
.
- 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 tompq_class(123_mpz)
. Other rationals can be built as-1_mpq/2
or0xb_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, exceptlong long
andlong 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 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::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 anmpq_class
. This can be passed to C functions expecting anmpz_t
. Any modifications made to thempz_t
will modify the originalmpq_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 asmpq_t operator>>
(see section C++ Formatted Input).If the rop read might not be in canonical form then
mpq_class::canonicalize
must be called.
[ << ] | [ < ] | [ Up ] | [ > ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
This document was generated on March 31, 2014 using texi2html 5.0.