A Rational Numbers Implementation as an Example For gf¶
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
Comparison also works as expected:
>>> R(1, 2) == R(2, 4)
True
>>> R(4, 2) == 2
True
>>> 1 == R(1, 2)
False
>>> 3 == R(10, 5)
False
>>> R(1, 2) < R(3, 4)
True
>>> R(1, 2) < 1
True
>>> R(1, 2) < 1
True
>>> R(1, 2) > R(1, 4)
True
>>> 1 > R(1, 2)
True
>>> 2 > 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(79864829273648218372937, 2500000000000000000000)
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
-
rational.
__add__
(*arguments)¶ Same as a + b.
Called by the
AbstractObject.__add__()
special method. Also called byAbstractObject.__radd__()
with arguments reversed.Multi methods:
-
gf.go.
__add__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__eq__
(*arguments)¶ Same as a == b.
Called by the
AbstractObject.__eq__()
special method.Multi methods:
-
gf.go.
__eq__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__float__
(*arguments)¶ Convert an
AbstractObject
to a float.Multi methods:
-
rational.
__ge__
(*arguments)¶ Same as a >= b.
Called by the
AbstractObject.__ge__()
special method.Multi methods:
-
gf.go.
__ge__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__gt__
(*arguments)¶ Same as a > b.
Called by the
AbstractObject.__gt__()
special method.Multi methods:
-
gf.go.
__gt__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__init__
(*arguments)[source]¶ __init__()
initializes instantiates instances ofAbstractObject
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 forAbstractObject
, therefore this class can not be instantiated.Multi methods:
-
gf.go.
__init__
(writer: Writer)¶ Initialize the Write with a StringIO object.
-
gf.go.
__init__
(writer: Writer, file_like: object)¶ Initialize the Write with a file like object.
- param file_like
A file-like object.
-
rational.
__init__
(rational: Rational, numerator: int, denominator: int, cancel: bool)¶ 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.
-
rational.
__init__
(rational: Rational, numerator: int, denominator: int)¶ - Initialize the object with numerator and denominator.
- 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.
-
rational.
__init__
(rational: Rational, numerator: int)¶ - Initialize the object with numerator.
- param rational
The rational number to be initialized.
- param numerator
The numerator.
Call
__init__()
with the denominator set to 1.
-
rational.
__init__
(rational: Rational)¶ - Initialize the object to be 0.
- param rational
The rational number to be initialized.
Call
__init__()
with the numerator set to 0.
-
rational.
__init__
(rational0: Rational, rational1: Rational)¶ - Initialize the object from another rational.
- param rational0
The rational number to be initialized.
- param rational1
The rational number the attributes are copied from.
-
rational.
__init__
(rational0: Rational, rational1: Rational, rational2: Rational)¶ - Initialize the object from another rational.
- 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.
-
rational.
__init__
(rational: Rational, decimal: Decimal)¶ - Initialize the object from a
decimal.Decimal
. - 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.
- Initialize the object from a
-
-
rational.
__le__
(*arguments)¶ Same as a <= b.
Called by the
AbstractObject.__le__()
special method.Multi methods:
-
gf.go.
__le__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__lt__
(*arguments)¶ Same as a < b.
Called by the
AbstractObject.__lt__()
special method.Multi methods:
-
gf.go.
__lt__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__mul__
(*arguments)¶ Same as a * b.
Called by the
AbstractObject.__mul__()
special method. Also called byAbstractObject.__rmul__()
with arguments reversed.Multi methods:
-
gf.go.
__mul__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__ne__
(*arguments)¶ Same as a != b.
Called by the
AbstractObject.__ne__()
special method.Multi methods:
-
gf.go.
__ne__
(o0: object, o1: object)¶ Defaults to not comparable.
-
-
rational.
__neg__
(*arguments)¶ Same as -a.
Called by the
AbstractObject.__neg__()
special method.Multi methods:
-
rational.
__out__
(*arguments)[source]¶ Create a print string of an object using a
Writer
.Multi methods:
-
gf.go.
__out__
(self: object, write: Writer)¶ Write a just
str()
of self.
-
gf.go.
__out__
(self: AbstractObject, write: Writer)¶ Write a just
str()
of self by directly callingobject.__str__()
.
-
-
rational.
__spy__
(*arguments)[source]¶ 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:
-
gf.go.
__spy__
(self: object, write: Writer)¶ Write a just
repr()
of self.
-
gf.go.
__spy__
(self: AbstractObject, write: Writer)¶ Write a just
repr()
of self by directly callingobject.__repr__()
.
-
-
rational.
__sub__
(*arguments)¶ Same as a - b.
Called by the
AbstractObject.__sub__()
special method. Also called byAbstractObject.__rsub__()
with arguments reversed.Multi methods:
-
gf.go.
__sub__
(o0: object, o1: object)¶ Defaults to not comparable.
-