1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
|
Multiple-precision Rationals
============================
gmpy2 provides a rational type call *mpq*. It should be a replacement for
Python's fractions.Fraction module.
::
>>> import gmpy2
>>> from gmpy2 import mpq
>>> mpq(1,7)
mpq(1,7)
>>> mpq(1,7) * 11
mpq(11,7)
>>> mpq(11,7)/13
mpq(11,91)
mpq Methods
-----------
**digits(...)**
x.digits([base=10]) returns a Python string representing *x* in the
given base (2 to 62, default is 10). A leading '-' is present if *x* < 0,
but no leading '+' is present if *x* >= 0.
mpq Attributes
--------------
**denominator**
x.denomintor returns the denominator of *x*.
**numerator**
x.numerator returns the numerator of *x*.
mpq Functions
-------------
**add(...)**
add(x, y) returns *x* + *y*. The result type depends on the input
types.
**div(...)**
div(x, y) returns *x* / *y*. The result type depends on the input
types.
**f2q(...)**
f2q(x[, err]) returns the best *mpq* approximating *x* to within
relative error *err*. Default is the precision of *x*. If *x* is not an
*mpfr*, it is converted to an *mpfr*. Uses Stern-Brocot tree to find the
best approximation. An *mpz* is returned if the the denominator is 1. If
*err* < 0, then the relative error sought is 2.0 ** *err*.
**mpq(...)**
mpq() returns an *mpq* object set to 0/1.
mpq(n) returns an *mpq* object with a numeric value *n*. Decimal and
Fraction values are converted exactly.
mpq(n, m) returns an *mpq* object with a numeric value *n* / *m*.
mpq(s[, base=10]) returns an *mpq* object from a string *s* made up of
digits in the given base. *s* may be made up of two numbers in the same
base separated by a '/' character. If *base* == 10, then an embedded '.'
indicates a number with a decimal fractional part.
**mul(...)**
mul(x, y) returns *x* \* *y*. The result type depends on the input
types.
**qdiv(...)**
qdiv(x[, y=1]) returns *x/y* as *mpz* if possible, or as *mpq* if *x*
is not exactly divisible by *y*.
**sub(...)**
sub(x, y) returns *x* - *y*. The result type depends on the input
types.
|