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

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
rational.gcd(a, b)[source]

gcd() computes GCD of to numbers.

class rational.Rational(*arguments)[source]

Rational is our rational numbers class.

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__(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: long, denominator: long, 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.

Use gf.__init__.super() to call the multi-method that has the (Rational, int, int, bool) signature.

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: long, denominator: long)
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, denominator: long)
Initialize the object with numerator and denominator.
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.

rational.__init__(rational: Rational, numerator: long, 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.

Convert the denominator to long and call __init__() with all arguments.

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, numerator: long)
Initialize the object with numerator.
param rational:The rational number to be initialized.
param numerator:
 The numerator.

Call __init__() with the denominator set to 1L.

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

Convert an AbstractObject to a float.

Multi methods:

rational.__float__(rational: Rational)

Convert a rational to a float.

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

add(a, b) – Same as a + b.

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

Multi methods:

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

sub(a, b) – Same as a - b.

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

Multi methods:

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

mul(a, b) – Same as a * b.

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

Multi methods:

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: Rational, b: object)

Multiply a rational and an object.

b is converted to a Rational and then both are multiplied.
rational.__div__(*arguments)[source]

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:

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

Divide two rational numbers.

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

Divide an object and a rational number.

a is converted to a Rational and then both are divided.
rational.__div__(a: Rational, b: object)

Divide a rational and an object.

b is converted to a Rational and then both are divided.
rational.__neg__(*arguments)[source]

neg(a) – Same as -a.

Called by the AbstractObject.__neg__() special method.

Multi methods:

rational.__neg__(rational: Rational)

Negate a rational number.

rational.__eq__(*arguments)[source]

eq(a, b) – Same as a==b.

Called by the AbstractObject.__eq__() special method.

Multi methods:

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

Compare a rational numbers and a long for equality.

Note:This is an optimisation for long.
rational.__ne__(*arguments)[source]

ne(a, b) – Same as a!=b.

Called by the AbstractObject.__ne__() special method.

Multi methods:

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

lt(a, b) – Same as a<b.

Called by the AbstractObject.__lt__() special method.

Multi methods:

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

le(a, b) – Same as a<=b.

Called by the AbstractObject.__le__() special method.

Multi methods:

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

gt(a, b) – Same as a>b.

Called by the AbstractObject.__gt__() special method.

Multi methods:

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

ge(a, b) – Same as a>=b.

Called by the AbstractObject.__ge__() special method.

Multi methods:

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.

Previous topic

Application Programming Interface

Next topic

Acknowledgements

This Page