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
class rational.Rational(*arguments)[source]

Rational is our rational numbers class.

rational.__add__(*arguments)

Same as a + b.

Called by the AbstractObject.__add__() special method. Also called by AbstractObject.__radd__() with arguments reversed.

Multi methods:

gf.go.__add__(o0: object, o1: object)

Defaults to not comparable.

rational.__add__(a: Rational, b: Rational)

Add two rational numbers.

rational.__add__(a: object, b: Rational)

Add an object and a rational number.

a is converted to a Rational and then both are added.

rational.__add__(a: Rational, b: object)

Add a rational number and an object.

b is converted to a Rational and then both are added.

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.__eq__(a: Rational, b: Rational)

Compare to rational numbers for equality.

rational.__eq__(a: Rational, b: object)

Compare a rational numbers and another object for equality.

rational.__eq__(a: Rational, b: int)

Compare a rational numbers and an integer for equality.

Note

This is an optimisation for int.

rational.__float__(*arguments)

Convert an AbstractObject to a float.

Multi methods:

rational.__float__(rational: Rational)

Convert a rational to a float.

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.__ge__(a: Rational, b: Rational)

Answer True if a is bigger or equal than b.

rational.__ge__(a: Rational, b: object)

Answer True if a is bigger or equal than b.

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.__gt__(a: Rational, b: Rational)

Answer True if a is bigger than b.

rational.__gt__(a: Rational, b: object)

Answer True if a is bigger than b.

rational.__init__(*arguments)[source]

__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:

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.

gf.go.__init__(an_object: Object)

Do nothing for 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.

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.__le__(a: Rational, b: Rational)

Answer True if a is smaller than or equal b.

rational.__le__(a: Rational, b: object)

Answer True if a is smaller than or equal b.

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.__lt__(a: Rational, b: Rational)

Answer True if a is smaller than b.

rational.__lt__(a: Rational, b: object)

Answer True if a is smaller than b.

rational.__mul__(*arguments)

Same as a * b.

Called by the AbstractObject.__mul__() special method. Also called by AbstractObject.__rmul__() with arguments reversed.

Multi methods:

gf.go.__mul__(o0: object, o1: object)

Defaults to not comparable.

rational.__mul__(a: Rational, b: Rational)

Multiply two rational numbers.

rational.__mul__(a: object, b: Rational)

Multiply an object and a rational number.

a is converted to a Rational and then both are multiplied.

rational.__mul__(a: object, b: Rational)

Multiply a rational and an object.

b is converted to a Rational and then both are multiplied.

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.__ne__(a: Rational, b: Rational)

Compare to rational numbers for inequality.

rational.__ne__(a: Rational, b: object)

Compare to rational numbers for inequality.

rational.__neg__(*arguments)

Same as -a.

Called by the AbstractObject.__neg__() special method.

Multi methods:

rational.__neg__(rational: Rational)

Negate a rational number.

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 calling object.__str__().

rational.__out__(rational: Rational, writer: Writer)

Write a nice representation of the rational.

Denominators that equal 1 are not printed.

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 calling object.__repr__().

rational.__spy__(rational: Rational, writer: Writer)

Write a debug representation of the rational.

rational.__sub__(*arguments)

Same as a - b.

Called by the AbstractObject.__sub__() special method. Also called by AbstractObject.__rsub__() with arguments reversed.

Multi methods:

gf.go.__sub__(o0: object, o1: object)

Defaults to not comparable.

rational.__sub__(a: Rational, b: Rational)

Subtract two rational numbers.

rational.__sub__(a: object, b: Rational)

Subtract an object and a rational number.

a is converted to a Rational and then both are subtracted.

rational.__sub__(a: Rational, b: object)

Subtract a rational number and an object.

b is converted to a Rational and then both are subtracted.

rational.gcd(a, b)[source]

gcd() computes GCD of to numbers.