The following text is taken from gf.examples.rational‘s inline documenation.
rational an Implementation of Rational Numbers
The module provides rational arithmetic. Additionally the module servers as example for the generic function package.
Usually you only need its Rational class:
>>> from rational import Rational as R
Rational numbers can be constructed from integers:
>>> r2 = R(1, 2)
>>> r1 = R(1)
>>> r0 = R()
Construction from arbitrary objects is not possible: >>> R(“Urmel”) # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... NotImplementedError: Generic ‘gf.go.__init__’ has no implementation for type(s): rational.Rational, __builtin__.str
Rationals also have a decent string representation:
>>> r0
Rational()
>>> print r0
0
>>> r1
Rational(1)
>>> print r1
1
>>> r2
Rational(1, 2)
>>> print r2
1 / 2
Ordinary arithmetic works as expected:
>>> print R(1, 2) + R(1, 4)
3 / 4
>>> 1 + R(1, 2)
Rational(3, 2)
>>> print R(2) / 1000
1 / 500
>>> print R(-5,-10)
1 / 2
>>> print R(5, -10)
-1 / 2
>>> print -R(5, -10)
1 / 2
Conversion from longs also works:
>>> R(1L)
Rational(1L)
Mixed constituents are converted to long:
>>> R(1, 2L)
Rational(1L, 2L)
Comparison also works as expected:
>>> R(1, 2) == R(2, 4)
True
>>> R(4, 2) == 2
True
>>> 1 == R(1, 2)
False
>>> 3L == R(10, 5)
False
>>> R(1, 2) < R(3, 4)
True
>>> R(1, 2) < 1
True
>>> R(1, 2) < 1L
True
>>> R(1, 2) > R(1, 4)
True
>>> 1 > R(1, 2)
True
>>> 2L > R(10, 7)
True
>>> R(10, 2) >= R(5)
True
>>> R() != R(1)
True
>>> R() != 0
False
>>> 1 != R(1)
False
The decimal module is supported as well:
>>> from decimal import Decimal as D
>>> R(D("0.375"))
Rational(3, 8)
>>> R(1, 2) + D("1.5")
Rational(2)
Even very long decimals do work:
>>> R(D("7.9864829273648218372937") * 4)
Rational(79864829273648218372937L, 2500000000000000000000L)
Comparisons with decimal.Decimal instances are also supported:
>>> D("1.2") == R(24, 20)
True
>>> D("1.2") >= R(23, 20)
True
>>> R(23, 20) <= D("1.2")
True
Rationals can also converted to floats:
>>> float(R(1, 4))
0.25
__init__() initializes instantiates instances of AbstractObject and it’s subclasses.
It has a multi method for Object. This multi-method does not accept any additional parameters and has no effect. There is no method for AbstractObject, therefore this class can not be instantiated.
Multi methods:
Do nothing for Object.
Initialize the object with numerator and denominator.
param rational: The rational number to be initialized. param numerator: The numerator. param denominator: The denominator. param cancel: A flag indicating, that numerator`and `denominator should be canceled.
Initialize the object with numerator and denominator.
param rational: The rational number to be initialized. param numerator: The numerator. param denominator: The denominator. param cancel: A flag indicating, that numerator`and `denominator should be canceled. Use gf.__init__.super() to call the multi-method that has the (Rational, int, int, bool) signature.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. | |
param denominator: | |
The denominator. |
Call __init__() with all passed arguments and with the value of CANCEL_EAGERLY for the cancel-flag.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. | |
param denominator: | |
The denominator. |
Call __init__() with all passed arguments and with the value of CANCEL_EAGERLY for the cancel-flag.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. | |
param denominator: | |
The denominator. |
Convert the numerator to long and call __init__() with all arguments.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. | |
param denominator: | |
The denominator. |
Convert the denominator to long and call __init__() with all arguments.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. |
Call __init__() with the denominator set to 1.
param rational: | The rational number to be initialized. |
---|---|
param numerator: | |
The numerator. |
Call __init__() with the denominator set to 1L.
param rational: | The rational number to be initialized. |
---|
Call __init__() with the numerator set to 0.
param rational0: | |
---|---|
The rational number to be initialized. | |
param rational1: | |
The rational number the attributes are copied from. |
param rational0: | |
---|---|
The rational number to be initialized. | |
param rational1: | |
The rational acting as numerator. | |
param rational2: | |
The rational acting as denominator. |
Call __init__() with rational0 as numerator and rational1 / rational2 as denominator.
param rational: | The rational number to be initialized. |
---|---|
param decimal: | The decimal number the rational is initialized from. |
If the decimal‘s exponent is negative compute a scaling denominator 10 ** -exponent and initialise rational with the decimal scaled by the denominator and the denominator.
In the other case the decimal is simply converted to an int and used as numerator.
Convert an AbstractObject to a float.
Multi methods:
Convert a rational to a float.
Create a print string of an object using a Writer.
Multi methods:
Write a just str() of self.
Write a just str() of self by directly calling object.__str__().
Write a nice representation of the rational.
Denominators that equal 1 are not printed.
Create a print string of an object using a Writer.
Note
The function’s name was taken from Prolog’s spy debugging aid.
Multi methods:
Write a just repr() of self.
Write a just repr() of self by directly calling object.__repr__().
Write a debug representation of the rational.
add(a, b) – Same as a + b.
Called by the AbstractObject.__add__() special method. Also called by AbstractObject.__radd__() with arguments reversed.
Multi methods:
Add two rational numbers.
Add an object and a rational number.
a is converted to a Rational and then both are added.
Add a rational number and an object.
b is converted to a Rational and then both are added.
sub(a, b) – Same as a - b.
Called by the AbstractObject.__sub__() special method. Also called by AbstractObject.__rsub__() with arguments reversed.
Multi methods:
Subtract two rational numbers.
Subtract an object and a rational number.
a is converted to a Rational and then both are subtracted.
Subtract a rational number and an object.
b is converted to a Rational and then both are subtracted.
mul(a, b) – Same as a * b.
Called by the AbstractObject.__mul__() special method. Also called by AbstractObject.__rmul__() with arguments reversed.
Multi methods:
Multiply two rational numbers.
Multiply an object and a rational number.
a is converted to a Rational and then both are multiplied.
Multiply a rational and an object.
b is converted to a Rational and then both are multiplied.
div(a, b) – Same as a / b when __future__.division is not in effect.
Called by the AbstractObject.__div__() special method. Also called by AbstractObject.__rdiv__() with arguments reversed.
Multi methods:
Divide two rational numbers.
Divide an object and a rational number.
a is converted to a Rational and then both are divided.
Divide a rational and an object.
b is converted to a Rational and then both are divided.
neg(a) – Same as -a.
Called by the AbstractObject.__neg__() special method.
Multi methods:
Negate a rational number.
eq(a, b) – Same as a==b.
Called by the AbstractObject.__eq__() special method.
Multi methods:
Compare to rational numbers for equality.
Compare a rational numbers and another object for equality.
Compare a rational numbers and an integer for equality.
Note: This is an optimisation for int.
Compare a rational numbers and a long for equality.
Note: This is an optimisation for long.
ne(a, b) – Same as a!=b.
Called by the AbstractObject.__ne__() special method.
Multi methods:
Compare to rational numbers for inequality.
Compare to rational numbers for inequality.
lt(a, b) – Same as a<b.
Called by the AbstractObject.__lt__() special method.
Multi methods:
Answer True if a is smaller than b.
Answer True if a is smaller than b.
le(a, b) – Same as a<=b.
Called by the AbstractObject.__le__() special method.
Multi methods:
Answer True if a is smaller than or equal b.
Answer True if a is smaller than or equal b.
gt(a, b) – Same as a>b.
Called by the AbstractObject.__gt__() special method.
Multi methods:
Answer True if a is bigger than b.
Answer True if a is bigger than b.
ge(a, b) – Same as a>=b.
Called by the AbstractObject.__ge__() special method.
Multi methods:
Answer True if a is bigger or equal than b.
Answer True if a is bigger or equal than b.