Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/rational.py
blob: f99f6103c3e97ec742c720e24ceb45f940c631e5 (plain)
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# rational.py, rational number class Reinier Heeres <reinier@heeres.eu>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
#
# Change log:
#    2007-07-03: rwh, first version

import types
from decimal import Decimal

import logging
_logger = logging.getLogger('Rational')

from gettext import gettext as _

class Rational:

    def __init__(self, n=None, d=None):
        self.n = 0
        self.d = 0

        if n is not None:
            self.set(n, d)

    def set(self, n, d=None):
        if d is not None:
            self.n = long(n)
            self.d = long(d)
        elif type(n) is types.TupleType or type(n) is types.ListType:
            self.n = long(rval[0])
            self.d = long(rval[1])
        elif type(n) == types.StringType:
            return

        self._simplify()

    def __str__(self):
        if self.d == 1 or self.d == 0:
            return "%d" % (self.n)
        else:
            return "%d/%d" % (self.n, self.d)
           
    def __float__(self):
        return float(self.n) / float(self.d)

    def gcd(self, a, b):
        if b == 0:
            return a
        else:
            return self.gcd(b, a % b)

    def _simplify(self):
        if self.d == 0:
            return

        if self.n == self.d:
            self.n = long(1)
            self.d = long(1)
        else:
            gcd = self.gcd(self.n, self.d)
            self.n /= gcd
            self.d /= gcd

    def __add__(self, rval):
        if isinstance(rval, Rational):
            ret = Rational(self.n * rval.d + self.d * rval.n, self.d * rval.d)
        elif type(rval) is types.IntType or type(rval) is types.LongType:
            ret = Rational(self.n + self.d * rval, self.d)
        else:
            ret = float(self) + rval
        return ret

    def __radd__(self, lval):
        return self.__add__(lval)

    def __sub__(self, rval):
        if isinstance(rval, Rational):
            ret = Rational(self.n * rval.d - self.d * rval.n, self.d * rval.d)
        elif type(rval) is types.IntType or type(rval) is types.LongType:
            ret = Rational(self.n - self.d * rval, self.d)
        else:
            ret = float(self) - rval
        return ret

    def __rsub__(self, lval):
        return -self.__sub__(lval)

    def __mul__(self, rval):
        if isinstance(rval, Rational):
            ret = Rational(self.n * rval.n, self.d * rval.d)
        elif type(rval) is types.IntType or type(rval) is types.LongType:
            ret = Rational(self.n * rval, self.d)
        elif isinstance(rval, Decimal):
            ret = rval * Decimal(str(float(self)))
        else:
            ret = rval * float(self)
        return ret

    def __rmul__(self, lval):
        return self.__mul__(lval)

    def __div__(self, rval):
        if isinstance(rval, Rational):
            ret = Rational(self.n * rval.d, self.d * rval.n)
        elif type(rval) is types.IntType or type(rval) is types.LongType:
            ret = Rational(self.n, self.d * rval)
        else:
            ret = float(self) / rval
        return ret

    def __rdiv__(self, lval):
        return self.__div__(lval)

    def __neg__(self):
        return Rational(-self.n, self.d)

    def __abs__(self):
        self.n = abs(self.n)
        self.d = abs(self.d)

    def __pow__(self, rval):
        if type(rval) is types.IntType or type(rval) is types.LongType:
            ret = Rational(self.n ** rval, self.d ** rval)
        else:
            ret = float(self.n) ** rval / float(self.d) ** rval

        return ret