Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 1 | # Originally contributed by Sjoerd Mullender. |
| 2 | # Significantly modified by Jeffrey Yasskin <jyasskin at gmail.com>. |
| 3 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 4 | """Fraction, infinite-precision, real numbers.""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 5 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 6 | from decimal import Decimal |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 7 | import math |
| 8 | import numbers |
| 9 | import operator |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 10 | import re |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 11 | import sys |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 12 | |
Victor Stinner | 4691a2f | 2020-01-16 11:02:51 +0100 | [diff] [blame] | 13 | __all__ = ['Fraction'] |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 14 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 15 | |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 16 | # Constants related to the hash implementation; hash(x) is based |
| 17 | # on the reduction of x modulo the prime _PyHASH_MODULUS. |
| 18 | _PyHASH_MODULUS = sys.hash_info.modulus |
| 19 | # Value to be used for rationals that reduce to infinity modulo |
| 20 | # _PyHASH_MODULUS. |
| 21 | _PyHASH_INF = sys.hash_info.inf |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 22 | |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 23 | _RATIONAL_FORMAT = re.compile(r""" |
| 24 | \A\s* # optional whitespace at the start, then |
| 25 | (?P<sign>[-+]?) # an optional sign, then |
| 26 | (?=\d|\.\d) # lookahead for digit or .digit |
| 27 | (?P<num>\d*) # numerator (possibly empty) |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 28 | (?: # followed by |
| 29 | (?:/(?P<denom>\d+))? # an optional denominator |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 30 | | # or |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 31 | (?:\.(?P<decimal>\d*))? # an optional fractional part |
| 32 | (?:E(?P<exp>[-+]?\d+))? # and optional exponent |
| 33 | ) |
Christian Heimes | 292d351 | 2008-02-03 16:51:08 +0000 | [diff] [blame] | 34 | \s*\Z # and optional whitespace to finish |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 35 | """, re.VERBOSE | re.IGNORECASE) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 36 | |
| 37 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 38 | class Fraction(numbers.Rational): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 39 | """This class implements rational numbers. |
| 40 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 41 | In the two-argument form of the constructor, Fraction(8, 6) will |
| 42 | produce a rational number equivalent to 4/3. Both arguments must |
| 43 | be Rational. The numerator defaults to 0 and the denominator |
| 44 | defaults to 1 so that Fraction(3) == 3 and Fraction() == 0. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 45 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 46 | Fractions can also be constructed from: |
| 47 | |
| 48 | - numeric strings similar to those accepted by the |
| 49 | float constructor (for example, '-2.3' or '1e10') |
| 50 | |
| 51 | - strings of the form '123/456' |
| 52 | |
| 53 | - float and Decimal instances |
| 54 | |
| 55 | - other Rational instances (including integers) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 56 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 57 | """ |
| 58 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 59 | __slots__ = ('_numerator', '_denominator') |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 60 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 61 | # We're immutable, so use __new__ not __init__ |
Mark Dickinson | 7caf908 | 2016-08-23 16:16:52 +0100 | [diff] [blame] | 62 | def __new__(cls, numerator=0, denominator=None, *, _normalize=True): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 63 | """Constructs a Rational. |
| 64 | |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 65 | Takes a string like '3/2' or '1.5', another Rational instance, a |
| 66 | numerator/denominator pair, or a float. |
| 67 | |
| 68 | Examples |
| 69 | -------- |
| 70 | |
| 71 | >>> Fraction(10, -8) |
| 72 | Fraction(-5, 4) |
| 73 | >>> Fraction(Fraction(1, 7), 5) |
| 74 | Fraction(1, 35) |
| 75 | >>> Fraction(Fraction(1, 7), Fraction(2, 3)) |
| 76 | Fraction(3, 14) |
| 77 | >>> Fraction('314') |
| 78 | Fraction(314, 1) |
| 79 | >>> Fraction('-35/4') |
| 80 | Fraction(-35, 4) |
| 81 | >>> Fraction('3.1415') # conversion from numeric string |
| 82 | Fraction(6283, 2000) |
| 83 | >>> Fraction('-47e-2') # string may include a decimal exponent |
| 84 | Fraction(-47, 100) |
| 85 | >>> Fraction(1.47) # direct construction from float (exact conversion) |
| 86 | Fraction(6620291452234629, 4503599627370496) |
| 87 | >>> Fraction(2.25) |
| 88 | Fraction(9, 4) |
| 89 | >>> Fraction(Decimal('1.47')) |
| 90 | Fraction(147, 100) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 91 | |
| 92 | """ |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 93 | self = super(Fraction, cls).__new__(cls) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 94 | |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 95 | if denominator is None: |
Georg Brandl | 40f9735 | 2014-09-24 08:37:55 +0200 | [diff] [blame] | 96 | if type(numerator) is int: |
| 97 | self._numerator = numerator |
| 98 | self._denominator = 1 |
| 99 | return self |
| 100 | |
| 101 | elif isinstance(numerator, numbers.Rational): |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 102 | self._numerator = numerator.numerator |
| 103 | self._denominator = numerator.denominator |
| 104 | return self |
| 105 | |
Serhiy Storchaka | 0d250bc | 2015-12-29 22:34:23 +0200 | [diff] [blame] | 106 | elif isinstance(numerator, (float, Decimal)): |
| 107 | # Exact conversion |
| 108 | self._numerator, self._denominator = numerator.as_integer_ratio() |
Mark Dickinson | 98127c3 | 2010-04-03 11:18:52 +0000 | [diff] [blame] | 109 | return self |
| 110 | |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 111 | elif isinstance(numerator, str): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 112 | # Handle construction from strings. |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 113 | m = _RATIONAL_FORMAT.match(numerator) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 114 | if m is None: |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 115 | raise ValueError('Invalid literal for Fraction: %r' % |
| 116 | numerator) |
| 117 | numerator = int(m.group('num') or '0') |
| 118 | denom = m.group('denom') |
| 119 | if denom: |
| 120 | denominator = int(denom) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 121 | else: |
Mark Dickinson | cf63f2f | 2009-04-22 17:50:21 +0000 | [diff] [blame] | 122 | denominator = 1 |
| 123 | decimal = m.group('decimal') |
| 124 | if decimal: |
| 125 | scale = 10**len(decimal) |
| 126 | numerator = numerator * scale + int(decimal) |
| 127 | denominator *= scale |
| 128 | exp = m.group('exp') |
| 129 | if exp: |
| 130 | exp = int(exp) |
| 131 | if exp >= 0: |
| 132 | numerator *= 10**exp |
| 133 | else: |
| 134 | denominator *= 10**-exp |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 135 | if m.group('sign') == '-': |
| 136 | numerator = -numerator |
| 137 | |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 138 | else: |
| 139 | raise TypeError("argument should be a string " |
| 140 | "or a Rational instance") |
| 141 | |
Georg Brandl | 40f9735 | 2014-09-24 08:37:55 +0200 | [diff] [blame] | 142 | elif type(numerator) is int is type(denominator): |
Serhiy Storchaka | 48e47aa | 2015-05-13 00:19:51 +0300 | [diff] [blame] | 143 | pass # *very* normal case |
Georg Brandl | 40f9735 | 2014-09-24 08:37:55 +0200 | [diff] [blame] | 144 | |
Mark Dickinson | d4d95f8 | 2009-04-24 14:06:19 +0000 | [diff] [blame] | 145 | elif (isinstance(numerator, numbers.Rational) and |
| 146 | isinstance(denominator, numbers.Rational)): |
| 147 | numerator, denominator = ( |
| 148 | numerator.numerator * denominator.denominator, |
| 149 | denominator.numerator * numerator.denominator |
| 150 | ) |
| 151 | else: |
| 152 | raise TypeError("both arguments should be " |
| 153 | "Rational instances") |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 155 | if denominator == 0: |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 156 | raise ZeroDivisionError('Fraction(%s, 0)' % numerator) |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 157 | if _normalize: |
Victor Stinner | dc7a50d | 2020-02-07 23:42:51 +0100 | [diff] [blame] | 158 | g = math.gcd(numerator, denominator) |
| 159 | if denominator < 0: |
| 160 | g = -g |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 161 | numerator //= g |
| 162 | denominator //= g |
| 163 | self._numerator = numerator |
| 164 | self._denominator = denominator |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 165 | return self |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 166 | |
| 167 | @classmethod |
| 168 | def from_float(cls, f): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 169 | """Converts a finite float to a rational number, exactly. |
| 170 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 171 | Beware that Fraction.from_float(0.3) != Fraction(3, 10). |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 172 | |
| 173 | """ |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 174 | if isinstance(f, numbers.Integral): |
Georg Brandl | 3a9b062 | 2009-01-03 22:07:57 +0000 | [diff] [blame] | 175 | return cls(f) |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 176 | elif not isinstance(f, float): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 177 | raise TypeError("%s.from_float() only takes floats, not %r (%s)" % |
| 178 | (cls.__name__, f, type(f).__name__)) |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 179 | return cls(*f.as_integer_ratio()) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 180 | |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 181 | @classmethod |
| 182 | def from_decimal(cls, dec): |
| 183 | """Converts a finite Decimal instance to a rational number, exactly.""" |
| 184 | from decimal import Decimal |
Georg Brandl | 2ee470f | 2008-07-16 12:55:28 +0000 | [diff] [blame] | 185 | if isinstance(dec, numbers.Integral): |
| 186 | dec = Decimal(int(dec)) |
| 187 | elif not isinstance(dec, Decimal): |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 188 | raise TypeError( |
| 189 | "%s.from_decimal() only takes Decimals, not %r (%s)" % |
| 190 | (cls.__name__, dec, type(dec).__name__)) |
Serhiy Storchaka | 0d250bc | 2015-12-29 22:34:23 +0200 | [diff] [blame] | 191 | return cls(*dec.as_integer_ratio()) |
Christian Heimes | 587c2bf | 2008-01-19 16:21:02 +0000 | [diff] [blame] | 192 | |
Raymond Hettinger | f03b4c8 | 2019-08-11 14:40:59 -0700 | [diff] [blame] | 193 | def as_integer_ratio(self): |
| 194 | """Return the integer ratio as a tuple. |
| 195 | |
| 196 | Return a tuple of two integers, whose ratio is equal to the |
| 197 | Fraction and with a positive denominator. |
| 198 | """ |
| 199 | return (self._numerator, self._denominator) |
| 200 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 201 | def limit_denominator(self, max_denominator=1000000): |
| 202 | """Closest Fraction to self with denominator at most max_denominator. |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 203 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 204 | >>> Fraction('3.141592653589793').limit_denominator(10) |
| 205 | Fraction(22, 7) |
| 206 | >>> Fraction('3.141592653589793').limit_denominator(100) |
| 207 | Fraction(311, 99) |
Mark Dickinson | dfb1562 | 2009-11-23 16:27:17 +0000 | [diff] [blame] | 208 | >>> Fraction(4321, 8765).limit_denominator(10000) |
| 209 | Fraction(4321, 8765) |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 210 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 211 | """ |
| 212 | # Algorithm notes: For any real number x, define a *best upper |
| 213 | # approximation* to x to be a rational number p/q such that: |
| 214 | # |
| 215 | # (1) p/q >= x, and |
| 216 | # (2) if p/q > r/s >= x then s > q, for any rational r/s. |
| 217 | # |
| 218 | # Define *best lower approximation* similarly. Then it can be |
| 219 | # proved that a rational number is a best upper or lower |
| 220 | # approximation to x if, and only if, it is a convergent or |
| 221 | # semiconvergent of the (unique shortest) continued fraction |
| 222 | # associated to x. |
| 223 | # |
| 224 | # To find a best rational approximation with denominator <= M, |
| 225 | # we find the best upper and lower approximations with |
| 226 | # denominator <= M and take whichever of these is closer to x. |
| 227 | # In the event of a tie, the bound with smaller denominator is |
| 228 | # chosen. If both denominators are equal (which can happen |
| 229 | # only when max_denominator == 1 and self is midway between |
| 230 | # two integers) the lower bound---i.e., the floor of self, is |
| 231 | # taken. |
| 232 | |
| 233 | if max_denominator < 1: |
| 234 | raise ValueError("max_denominator should be at least 1") |
| 235 | if self._denominator <= max_denominator: |
| 236 | return Fraction(self) |
| 237 | |
| 238 | p0, q0, p1, q1 = 0, 1, 1, 0 |
| 239 | n, d = self._numerator, self._denominator |
| 240 | while True: |
| 241 | a = n//d |
| 242 | q2 = q0+a*q1 |
| 243 | if q2 > max_denominator: |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 244 | break |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 245 | p0, q0, p1, q1 = p1, q1, p0+a*p1, q2 |
| 246 | n, d = d, n-a*d |
| 247 | |
| 248 | k = (max_denominator-q0)//q1 |
| 249 | bound1 = Fraction(p0+k*p1, q0+k*q1) |
| 250 | bound2 = Fraction(p1, q1) |
| 251 | if abs(bound2 - self) <= abs(bound1-self): |
| 252 | return bound2 |
| 253 | else: |
| 254 | return bound1 |
Christian Heimes | bbffeb6 | 2008-01-24 09:42:52 +0000 | [diff] [blame] | 255 | |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 256 | @property |
| 257 | def numerator(a): |
| 258 | return a._numerator |
| 259 | |
| 260 | @property |
| 261 | def denominator(a): |
| 262 | return a._denominator |
| 263 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 264 | def __repr__(self): |
| 265 | """repr(self)""" |
Serhiy Storchaka | 465e60e | 2014-07-25 23:36:00 +0300 | [diff] [blame] | 266 | return '%s(%s, %s)' % (self.__class__.__name__, |
| 267 | self._numerator, self._denominator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 268 | |
| 269 | def __str__(self): |
| 270 | """str(self)""" |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 271 | if self._denominator == 1: |
| 272 | return str(self._numerator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 273 | else: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 274 | return '%s/%s' % (self._numerator, self._denominator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 275 | |
| 276 | def _operator_fallbacks(monomorphic_operator, fallback_operator): |
| 277 | """Generates forward and reverse operators given a purely-rational |
| 278 | operator and a function from the operator module. |
| 279 | |
| 280 | Use this like: |
| 281 | __op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op) |
| 282 | |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 283 | In general, we want to implement the arithmetic operations so |
| 284 | that mixed-mode operations either call an implementation whose |
| 285 | author knew about the types of both arguments, or convert both |
| 286 | to the nearest built in type and do the operation there. In |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 287 | Fraction, that means that we define __add__ and __radd__ as: |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 288 | |
| 289 | def __add__(self, other): |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 290 | # Both types have numerators/denominator attributes, |
| 291 | # so do the operation directly |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 292 | if isinstance(other, (int, Fraction)): |
| 293 | return Fraction(self.numerator * other.denominator + |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 294 | other.numerator * self.denominator, |
| 295 | self.denominator * other.denominator) |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 296 | # float and complex don't have those operations, but we |
| 297 | # know about those types, so special case them. |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 298 | elif isinstance(other, float): |
| 299 | return float(self) + other |
| 300 | elif isinstance(other, complex): |
| 301 | return complex(self) + other |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 302 | # Let the other type take over. |
| 303 | return NotImplemented |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 304 | |
| 305 | def __radd__(self, other): |
| 306 | # radd handles more types than add because there's |
| 307 | # nothing left to fall back to. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 308 | if isinstance(other, numbers.Rational): |
| 309 | return Fraction(self.numerator * other.denominator + |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 310 | other.numerator * self.denominator, |
| 311 | self.denominator * other.denominator) |
| 312 | elif isinstance(other, Real): |
| 313 | return float(other) + float(self) |
| 314 | elif isinstance(other, Complex): |
| 315 | return complex(other) + complex(self) |
Christian Heimes | 400adb0 | 2008-02-01 08:12:03 +0000 | [diff] [blame] | 316 | return NotImplemented |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 317 | |
| 318 | |
| 319 | There are 5 different cases for a mixed-type addition on |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 320 | Fraction. I'll refer to all of the above code that doesn't |
| 321 | refer to Fraction, float, or complex as "boilerplate". 'r' |
| 322 | will be an instance of Fraction, which is a subtype of |
| 323 | Rational (r : Fraction <: Rational), and b : B <: |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 324 | Complex. The first three involve 'r + b': |
| 325 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 326 | 1. If B <: Fraction, int, float, or complex, we handle |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 327 | that specially, and all is well. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 328 | 2. If Fraction falls back to the boilerplate code, and it |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 329 | were to return a value from __add__, we'd miss the |
| 330 | possibility that B defines a more intelligent __radd__, |
| 331 | so the boilerplate should return NotImplemented from |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 332 | __add__. In particular, we don't handle Rational |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 333 | here, even though we could get an exact answer, in case |
| 334 | the other type wants to do something special. |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 335 | 3. If B <: Fraction, Python tries B.__radd__ before |
| 336 | Fraction.__add__. This is ok, because it was |
| 337 | implemented with knowledge of Fraction, so it can |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 338 | handle those instances before delegating to Real or |
| 339 | Complex. |
| 340 | |
| 341 | The next two situations describe 'b + r'. We assume that b |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 342 | didn't know about Fraction in its implementation, and that it |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 343 | uses similar boilerplate code: |
| 344 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 345 | 4. If B <: Rational, then __radd_ converts both to the |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 346 | builtin rational type (hey look, that's us) and |
| 347 | proceeds. |
| 348 | 5. Otherwise, __radd__ tries to find the nearest common |
| 349 | base ABC, and fall back to its builtin type. Since this |
| 350 | class doesn't subclass a concrete type, there's no |
| 351 | implementation to fall back to, so we need to try as |
| 352 | hard as possible to return an actual value, or the user |
| 353 | will get a TypeError. |
| 354 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 355 | """ |
| 356 | def forward(a, b): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 357 | if isinstance(b, (int, Fraction)): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 358 | return monomorphic_operator(a, b) |
| 359 | elif isinstance(b, float): |
| 360 | return fallback_operator(float(a), b) |
| 361 | elif isinstance(b, complex): |
| 362 | return fallback_operator(complex(a), b) |
| 363 | else: |
| 364 | return NotImplemented |
| 365 | forward.__name__ = '__' + fallback_operator.__name__ + '__' |
| 366 | forward.__doc__ = monomorphic_operator.__doc__ |
| 367 | |
| 368 | def reverse(b, a): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 369 | if isinstance(a, numbers.Rational): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 370 | # Includes ints. |
| 371 | return monomorphic_operator(a, b) |
| 372 | elif isinstance(a, numbers.Real): |
| 373 | return fallback_operator(float(a), float(b)) |
| 374 | elif isinstance(a, numbers.Complex): |
| 375 | return fallback_operator(complex(a), complex(b)) |
| 376 | else: |
| 377 | return NotImplemented |
| 378 | reverse.__name__ = '__r' + fallback_operator.__name__ + '__' |
| 379 | reverse.__doc__ = monomorphic_operator.__doc__ |
| 380 | |
| 381 | return forward, reverse |
| 382 | |
Sergey B Kirpichev | 690aca7 | 2021-03-22 05:30:55 +0300 | [diff] [blame] | 383 | # Rational arithmetic algorithms: Knuth, TAOCP, Volume 2, 4.5.1. |
| 384 | # |
| 385 | # Assume input fractions a and b are normalized. |
| 386 | # |
| 387 | # 1) Consider addition/subtraction. |
| 388 | # |
| 389 | # Let g = gcd(da, db). Then |
| 390 | # |
| 391 | # na nb na*db ± nb*da |
| 392 | # a ± b == -- ± -- == ------------- == |
| 393 | # da db da*db |
| 394 | # |
| 395 | # na*(db//g) ± nb*(da//g) t |
| 396 | # == ----------------------- == - |
| 397 | # (da*db)//g d |
| 398 | # |
| 399 | # Now, if g > 1, we're working with smaller integers. |
| 400 | # |
| 401 | # Note, that t, (da//g) and (db//g) are pairwise coprime. |
| 402 | # |
| 403 | # Indeed, (da//g) and (db//g) share no common factors (they were |
| 404 | # removed) and da is coprime with na (since input fractions are |
| 405 | # normalized), hence (da//g) and na are coprime. By symmetry, |
| 406 | # (db//g) and nb are coprime too. Then, |
| 407 | # |
| 408 | # gcd(t, da//g) == gcd(na*(db//g), da//g) == 1 |
| 409 | # gcd(t, db//g) == gcd(nb*(da//g), db//g) == 1 |
| 410 | # |
| 411 | # Above allows us optimize reduction of the result to lowest |
| 412 | # terms. Indeed, |
| 413 | # |
| 414 | # g2 = gcd(t, d) == gcd(t, (da//g)*(db//g)*g) == gcd(t, g) |
| 415 | # |
| 416 | # t//g2 t//g2 |
| 417 | # a ± b == ----------------------- == ---------------- |
| 418 | # (da//g)*(db//g)*(g//g2) (da//g)*(db//g2) |
| 419 | # |
| 420 | # is a normalized fraction. This is useful because the unnormalized |
| 421 | # denominator d could be much larger than g. |
| 422 | # |
| 423 | # We should special-case g == 1 (and g2 == 1), since 60.8% of |
| 424 | # randomly-chosen integers are coprime: |
| 425 | # https://en.wikipedia.org/wiki/Coprime_integers#Probability_of_coprimality |
| 426 | # Note, that g2 == 1 always for fractions, obtained from floats: here |
| 427 | # g is a power of 2 and the unnormalized numerator t is an odd integer. |
| 428 | # |
| 429 | # 2) Consider multiplication |
| 430 | # |
| 431 | # Let g1 = gcd(na, db) and g2 = gcd(nb, da), then |
| 432 | # |
| 433 | # na*nb na*nb (na//g1)*(nb//g2) |
| 434 | # a*b == ----- == ----- == ----------------- |
| 435 | # da*db db*da (db//g1)*(da//g2) |
| 436 | # |
| 437 | # Note, that after divisions we're multiplying smaller integers. |
| 438 | # |
| 439 | # Also, the resulting fraction is normalized, because each of |
| 440 | # two factors in the numerator is coprime to each of the two factors |
| 441 | # in the denominator. |
| 442 | # |
| 443 | # Indeed, pick (na//g1). It's coprime with (da//g2), because input |
| 444 | # fractions are normalized. It's also coprime with (db//g1), because |
| 445 | # common factors are removed by g1 == gcd(na, db). |
| 446 | # |
| 447 | # As for addition/subtraction, we should special-case g1 == 1 |
| 448 | # and g2 == 1 for same reason. That happens also for multiplying |
| 449 | # rationals, obtained from floats. |
| 450 | |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 451 | def _add(a, b): |
| 452 | """a + b""" |
Sergey B Kirpichev | 690aca7 | 2021-03-22 05:30:55 +0300 | [diff] [blame] | 453 | na, da = a.numerator, a.denominator |
| 454 | nb, db = b.numerator, b.denominator |
| 455 | g = math.gcd(da, db) |
| 456 | if g == 1: |
| 457 | return Fraction(na * db + da * nb, da * db, _normalize=False) |
| 458 | s = da // g |
| 459 | t = na * (db // g) + nb * s |
| 460 | g2 = math.gcd(t, g) |
| 461 | if g2 == 1: |
| 462 | return Fraction(t, s * db, _normalize=False) |
| 463 | return Fraction(t // g2, s * (db // g2), _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 464 | |
| 465 | __add__, __radd__ = _operator_fallbacks(_add, operator.add) |
| 466 | |
| 467 | def _sub(a, b): |
| 468 | """a - b""" |
Sergey B Kirpichev | 690aca7 | 2021-03-22 05:30:55 +0300 | [diff] [blame] | 469 | na, da = a.numerator, a.denominator |
| 470 | nb, db = b.numerator, b.denominator |
| 471 | g = math.gcd(da, db) |
| 472 | if g == 1: |
| 473 | return Fraction(na * db - da * nb, da * db, _normalize=False) |
| 474 | s = da // g |
| 475 | t = na * (db // g) - nb * s |
| 476 | g2 = math.gcd(t, g) |
| 477 | if g2 == 1: |
| 478 | return Fraction(t, s * db, _normalize=False) |
| 479 | return Fraction(t // g2, s * (db // g2), _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 480 | |
| 481 | __sub__, __rsub__ = _operator_fallbacks(_sub, operator.sub) |
| 482 | |
| 483 | def _mul(a, b): |
| 484 | """a * b""" |
Sergey B Kirpichev | 690aca7 | 2021-03-22 05:30:55 +0300 | [diff] [blame] | 485 | na, da = a.numerator, a.denominator |
| 486 | nb, db = b.numerator, b.denominator |
| 487 | g1 = math.gcd(na, db) |
| 488 | if g1 > 1: |
| 489 | na //= g1 |
| 490 | db //= g1 |
| 491 | g2 = math.gcd(nb, da) |
| 492 | if g2 > 1: |
| 493 | nb //= g2 |
| 494 | da //= g2 |
| 495 | return Fraction(na * nb, db * da, _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 496 | |
| 497 | __mul__, __rmul__ = _operator_fallbacks(_mul, operator.mul) |
| 498 | |
| 499 | def _div(a, b): |
| 500 | """a / b""" |
Sergey B Kirpichev | 690aca7 | 2021-03-22 05:30:55 +0300 | [diff] [blame] | 501 | # Same as _mul(), with inversed b. |
| 502 | na, da = a.numerator, a.denominator |
| 503 | nb, db = b.numerator, b.denominator |
| 504 | g1 = math.gcd(na, nb) |
| 505 | if g1 > 1: |
| 506 | na //= g1 |
| 507 | nb //= g1 |
| 508 | g2 = math.gcd(db, da) |
| 509 | if g2 > 1: |
| 510 | da //= g2 |
| 511 | db //= g2 |
| 512 | n, d = na * db, nb * da |
| 513 | if d < 0: |
| 514 | n, d = -n, -d |
| 515 | return Fraction(n, d, _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 516 | |
| 517 | __truediv__, __rtruediv__ = _operator_fallbacks(_div, operator.truediv) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 518 | |
Elias Zamaria | 393f1ff | 2018-08-26 23:59:28 -0700 | [diff] [blame] | 519 | def _floordiv(a, b): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 520 | """a // b""" |
Stefan Behnel | 3a374e0 | 2019-01-02 13:22:06 +0100 | [diff] [blame] | 521 | return (a.numerator * b.denominator) // (a.denominator * b.numerator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 522 | |
Elias Zamaria | 393f1ff | 2018-08-26 23:59:28 -0700 | [diff] [blame] | 523 | __floordiv__, __rfloordiv__ = _operator_fallbacks(_floordiv, operator.floordiv) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 524 | |
Stefan Behnel | 3a374e0 | 2019-01-02 13:22:06 +0100 | [diff] [blame] | 525 | def _divmod(a, b): |
| 526 | """(a // b, a % b)""" |
| 527 | da, db = a.denominator, b.denominator |
| 528 | div, n_mod = divmod(a.numerator * db, da * b.numerator) |
| 529 | return div, Fraction(n_mod, da * db) |
| 530 | |
| 531 | __divmod__, __rdivmod__ = _operator_fallbacks(_divmod, divmod) |
| 532 | |
Elias Zamaria | 393f1ff | 2018-08-26 23:59:28 -0700 | [diff] [blame] | 533 | def _mod(a, b): |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 534 | """a % b""" |
Stefan Behnel | 3a374e0 | 2019-01-02 13:22:06 +0100 | [diff] [blame] | 535 | da, db = a.denominator, b.denominator |
| 536 | return Fraction((a.numerator * db) % (b.numerator * da), da * db) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 537 | |
Elias Zamaria | 393f1ff | 2018-08-26 23:59:28 -0700 | [diff] [blame] | 538 | __mod__, __rmod__ = _operator_fallbacks(_mod, operator.mod) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 539 | |
| 540 | def __pow__(a, b): |
| 541 | """a ** b |
| 542 | |
| 543 | If b is not an integer, the result will be a float or complex |
| 544 | since roots are generally irrational. If b is an integer, the |
| 545 | result will be rational. |
| 546 | |
| 547 | """ |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 548 | if isinstance(b, numbers.Rational): |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 549 | if b.denominator == 1: |
| 550 | power = b.numerator |
| 551 | if power >= 0: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 552 | return Fraction(a._numerator ** power, |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 553 | a._denominator ** power, |
| 554 | _normalize=False) |
Mark Dickinson | 8447965 | 2016-08-22 10:50:53 +0100 | [diff] [blame] | 555 | elif a._numerator >= 0: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 556 | return Fraction(a._denominator ** -power, |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 557 | a._numerator ** -power, |
| 558 | _normalize=False) |
Mark Dickinson | 8447965 | 2016-08-22 10:50:53 +0100 | [diff] [blame] | 559 | else: |
| 560 | return Fraction((-a._denominator) ** -power, |
| 561 | (-a._numerator) ** -power, |
| 562 | _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 563 | else: |
| 564 | # A fractional power will generally produce an |
| 565 | # irrational number. |
| 566 | return float(a) ** float(b) |
| 567 | else: |
| 568 | return float(a) ** b |
| 569 | |
| 570 | def __rpow__(b, a): |
| 571 | """a ** b""" |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 572 | if b._denominator == 1 and b._numerator >= 0: |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 573 | # If a is an int, keep it that way if possible. |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 574 | return a ** b._numerator |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 575 | |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 576 | if isinstance(a, numbers.Rational): |
| 577 | return Fraction(a.numerator, a.denominator) ** b |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 578 | |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 579 | if b._denominator == 1: |
| 580 | return a ** b._numerator |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 581 | |
| 582 | return a ** float(b) |
| 583 | |
| 584 | def __pos__(a): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 585 | """+a: Coerces a subclass instance to Fraction""" |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 586 | return Fraction(a._numerator, a._denominator, _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 587 | |
| 588 | def __neg__(a): |
| 589 | """-a""" |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 590 | return Fraction(-a._numerator, a._denominator, _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 591 | |
| 592 | def __abs__(a): |
| 593 | """abs(a)""" |
Mark Dickinson | 3c286e2 | 2014-04-05 09:29:00 +0100 | [diff] [blame] | 594 | return Fraction(abs(a._numerator), a._denominator, _normalize=False) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 595 | |
| 596 | def __trunc__(a): |
| 597 | """trunc(a)""" |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 598 | if a._numerator < 0: |
| 599 | return -(-a._numerator // a._denominator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 600 | else: |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 601 | return a._numerator // a._denominator |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 602 | |
| 603 | def __floor__(a): |
Jakub Molinski | a9a2880 | 2019-04-15 14:37:04 +0200 | [diff] [blame] | 604 | """math.floor(a)""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 605 | return a.numerator // a.denominator |
| 606 | |
| 607 | def __ceil__(a): |
Jakub Molinski | a9a2880 | 2019-04-15 14:37:04 +0200 | [diff] [blame] | 608 | """math.ceil(a)""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 609 | # The negations cleverly convince floordiv to return the ceiling. |
| 610 | return -(-a.numerator // a.denominator) |
| 611 | |
| 612 | def __round__(self, ndigits=None): |
Jakub Molinski | a9a2880 | 2019-04-15 14:37:04 +0200 | [diff] [blame] | 613 | """round(self, ndigits) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 614 | |
| 615 | Rounds half toward even. |
| 616 | """ |
| 617 | if ndigits is None: |
| 618 | floor, remainder = divmod(self.numerator, self.denominator) |
| 619 | if remainder * 2 < self.denominator: |
| 620 | return floor |
| 621 | elif remainder * 2 > self.denominator: |
| 622 | return floor + 1 |
| 623 | # Deal with the half case: |
| 624 | elif floor % 2 == 0: |
| 625 | return floor |
| 626 | else: |
| 627 | return floor + 1 |
| 628 | shift = 10**abs(ndigits) |
| 629 | # See _operator_fallbacks.forward to check that the results of |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 630 | # these operations will always be Fraction and therefore have |
Jeffrey Yasskin | 9893de1 | 2008-01-17 07:36:30 +0000 | [diff] [blame] | 631 | # round(). |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 632 | if ndigits > 0: |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 633 | return Fraction(round(self * shift), shift) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 634 | else: |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 635 | return Fraction(round(self / shift) * shift) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 636 | |
| 637 | def __hash__(self): |
Mark Dickinson | fec6620 | 2010-11-13 10:27:38 +0000 | [diff] [blame] | 638 | """hash(self)""" |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 639 | |
Raymond Hettinger | f3cb68f | 2019-08-15 20:58:26 -0700 | [diff] [blame] | 640 | # To make sure that the hash of a Fraction agrees with the hash |
| 641 | # of a numerically equal integer, float or Decimal instance, we |
| 642 | # follow the rules for numeric hashes outlined in the |
| 643 | # documentation. (See library docs, 'Built-in Types'). |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 644 | |
Raymond Hettinger | f3cb68f | 2019-08-15 20:58:26 -0700 | [diff] [blame] | 645 | try: |
| 646 | dinv = pow(self._denominator, -1, _PyHASH_MODULUS) |
| 647 | except ValueError: |
Tim Peters | 29bb227 | 2019-08-16 21:09:16 -0500 | [diff] [blame] | 648 | # ValueError means there is no modular inverse. |
Mark Dickinson | dc787d2 | 2010-05-23 13:33:13 +0000 | [diff] [blame] | 649 | hash_ = _PyHASH_INF |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 650 | else: |
Tim Peters | 29bb227 | 2019-08-16 21:09:16 -0500 | [diff] [blame] | 651 | # The general algorithm now specifies that the absolute value of |
| 652 | # the hash is |
| 653 | # (|N| * dinv) % P |
| 654 | # where N is self._numerator and P is _PyHASH_MODULUS. That's |
| 655 | # optimized here in two ways: first, for a non-negative int i, |
| 656 | # hash(i) == i % P, but the int hash implementation doesn't need |
| 657 | # to divide, and is faster than doing % P explicitly. So we do |
| 658 | # hash(|N| * dinv) |
| 659 | # instead. Second, N is unbounded, so its product with dinv may |
| 660 | # be arbitrarily expensive to compute. The final answer is the |
| 661 | # same if we use the bounded |N| % P instead, which can again |
| 662 | # be done with an int hash() call. If 0 <= i < P, hash(i) == i, |
| 663 | # so this nested hash() call wastes a bit of time making a |
| 664 | # redundant copy when |N| < P, but can save an arbitrarily large |
| 665 | # amount of computation for large |N|. |
| 666 | hash_ = hash(hash(abs(self._numerator)) * dinv) |
Raymond Hettinger | f3cb68f | 2019-08-15 20:58:26 -0700 | [diff] [blame] | 667 | result = hash_ if self._numerator >= 0 else -hash_ |
Mark Dickinson | fec6620 | 2010-11-13 10:27:38 +0000 | [diff] [blame] | 668 | return -2 if result == -1 else result |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 669 | |
| 670 | def __eq__(a, b): |
| 671 | """a == b""" |
Georg Brandl | 40f9735 | 2014-09-24 08:37:55 +0200 | [diff] [blame] | 672 | if type(b) is int: |
| 673 | return a._numerator == b and a._denominator == 1 |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 674 | if isinstance(b, numbers.Rational): |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 675 | return (a._numerator == b.numerator and |
| 676 | a._denominator == b.denominator) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 677 | if isinstance(b, numbers.Complex) and b.imag == 0: |
| 678 | b = b.real |
| 679 | if isinstance(b, float): |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 680 | if math.isnan(b) or math.isinf(b): |
| 681 | # comparisons with an infinity or nan should behave in |
| 682 | # the same way for any finite a, so treat a as zero. |
| 683 | return 0.0 == b |
| 684 | else: |
| 685 | return a == a.from_float(b) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 686 | else: |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 687 | # Since a doesn't know how to compare with b, let's give b |
| 688 | # a chance to compare itself with a. |
| 689 | return NotImplemented |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 690 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 691 | def _richcmp(self, other, op): |
| 692 | """Helper for comparison operators, for internal use only. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 693 | |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 694 | Implement comparison between a Rational instance `self`, and |
| 695 | either another Rational instance or a float `other`. If |
| 696 | `other` is not a Rational instance or a float, return |
| 697 | NotImplemented. `op` should be one of the six standard |
| 698 | comparison operators. |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 699 | |
| 700 | """ |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 701 | # convert other to a Rational instance where reasonable. |
| 702 | if isinstance(other, numbers.Rational): |
| 703 | return op(self._numerator * other.denominator, |
| 704 | self._denominator * other.numerator) |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 705 | if isinstance(other, float): |
| 706 | if math.isnan(other) or math.isinf(other): |
| 707 | return op(0.0, other) |
| 708 | else: |
| 709 | return op(self, self.from_float(other)) |
| 710 | else: |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 711 | return NotImplemented |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 712 | |
| 713 | def __lt__(a, b): |
| 714 | """a < b""" |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 715 | return a._richcmp(b, operator.lt) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 716 | |
| 717 | def __gt__(a, b): |
| 718 | """a > b""" |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 719 | return a._richcmp(b, operator.gt) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 720 | |
| 721 | def __le__(a, b): |
| 722 | """a <= b""" |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 723 | return a._richcmp(b, operator.le) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 724 | |
| 725 | def __ge__(a, b): |
| 726 | """a >= b""" |
Mark Dickinson | 85424c9 | 2009-07-18 14:41:42 +0000 | [diff] [blame] | 727 | return a._richcmp(b, operator.ge) |
Guido van Rossum | 7736b5b | 2008-01-15 21:44:53 +0000 | [diff] [blame] | 728 | |
| 729 | def __bool__(a): |
| 730 | """a != 0""" |
Sebastian Berg | 427c84f | 2020-02-06 06:54:05 -0800 | [diff] [blame] | 731 | # bpo-39274: Use bool() because (a._numerator != 0) can return an |
| 732 | # object which is not a bool. |
| 733 | return bool(a._numerator) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 734 | |
| 735 | # support for pickling, copy, and deepcopy |
| 736 | |
| 737 | def __reduce__(self): |
| 738 | return (self.__class__, (str(self),)) |
| 739 | |
| 740 | def __copy__(self): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 741 | if type(self) == Fraction: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 742 | return self # I'm immutable; therefore I am my own clone |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 743 | return self.__class__(self._numerator, self._denominator) |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 744 | |
| 745 | def __deepcopy__(self, memo): |
Christian Heimes | 3feef61 | 2008-02-11 06:19:17 +0000 | [diff] [blame] | 746 | if type(self) == Fraction: |
Christian Heimes | 969fe57 | 2008-01-25 11:23:10 +0000 | [diff] [blame] | 747 | return self # My components are also immutable |
Christian Heimes | 68f5fbe | 2008-02-14 08:27:37 +0000 | [diff] [blame] | 748 | return self.__class__(self._numerator, self._denominator) |