1 # Originally contributed by Sjoerd Mullender.
2 # Significantly modified by Jeffrey Yasskin <jyasskin at gmail.com>.
4 """Rational, infinite-precision, real numbers."""
6 from __future__
import division
12 __all__
= ['Fraction', 'gcd']
14 Rational
= numbers
.Rational
18 """Calculate the Greatest Common Divisor of a and b.
20 Unless b==0, the result will have the same sign as b (so that when
21 b is divided by it, the result comes out positive).
28 _RATIONAL_FORMAT
= re
.compile(r
"""
29 \A\s* # optional whitespace at the start, then
30 (?P<sign>[-+]?) # an optional sign, then
31 (?=\d|\.\d) # lookahead for digit or .digit
32 (?P<num>\d*) # numerator (possibly empty)
33 (?: # followed by an optional
34 /(?P<denom>\d+) # / and denominator
36 \.(?P<decimal>\d*) # decimal point and fractional part
38 \s*\Z # and optional whitespace to finish
42 class Fraction(Rational
):
43 """This class implements rational numbers.
45 Fraction(8, 6) will produce a rational number equivalent to
46 4/3. Both arguments must be Integral. The numerator defaults to 0
47 and the denominator defaults to 1 so that Fraction(3) == 3 and
50 Fractions can also be constructed from strings of the form
51 '[-+]?[0-9]+((/|.)[0-9]+)?', optionally surrounded by spaces.
55 __slots__
= ('_numerator', '_denominator')
57 # We're immutable, so use __new__ not __init__
58 def __new__(cls
, numerator
=0, denominator
=1):
59 """Constructs a Fraction.
61 Takes a string like '3/2' or '1.5', another Fraction, or a
62 numerator/denominator pair.
65 self
= super(Fraction
, cls
).__new
__(cls
)
67 if type(numerator
) not in (int, long) and denominator
== 1:
68 if isinstance(numerator
, basestring
):
69 # Handle construction from strings.
71 m
= _RATIONAL_FORMAT
.match(input)
73 raise ValueError('Invalid literal for Fraction: %r' % input)
74 numerator
= m
.group('num')
75 decimal
= m
.group('decimal')
77 # The literal is a decimal number.
78 numerator
= int(numerator
+ decimal
)
79 denominator
= 10**len(decimal
)
81 # The literal is an integer or fraction.
82 numerator
= int(numerator
)
83 # Default denominator to 1.
84 denominator
= int(m
.group('denom') or 1)
86 if m
.group('sign') == '-':
87 numerator
= -numerator
89 elif isinstance(numerator
, Rational
):
90 # Handle copies from other rationals. Integrals get
91 # caught here too, but it doesn't matter because
92 # denominator is already 1.
93 other_rational
= numerator
94 numerator
= other_rational
.numerator
95 denominator
= other_rational
.denominator
98 raise ZeroDivisionError('Fraction(%s, 0)' % numerator
)
99 numerator
= operator
.index(numerator
)
100 denominator
= operator
.index(denominator
)
101 g
= gcd(numerator
, denominator
)
102 self
._numerator
= numerator
// g
103 self
._denominator
= denominator
// g
107 def from_float(cls
, f
):
108 """Converts a finite float to a rational number, exactly.
110 Beware that Fraction.from_float(0.3) != Fraction(3, 10).
113 if isinstance(f
, numbers
.Integral
):
115 elif not isinstance(f
, float):
116 raise TypeError("%s.from_float() only takes floats, not %r (%s)" %
117 (cls
.__name
__, f
, type(f
).__name
__))
118 if math
.isnan(f
) or math
.isinf(f
):
119 raise TypeError("Cannot convert %r to %s." % (f
, cls
.__name
__))
120 return cls(*f
.as_integer_ratio())
123 def from_decimal(cls
, dec
):
124 """Converts a finite Decimal instance to a rational number, exactly."""
125 from decimal
import Decimal
126 if isinstance(dec
, numbers
.Integral
):
127 dec
= Decimal(int(dec
))
128 elif not isinstance(dec
, Decimal
):
130 "%s.from_decimal() only takes Decimals, not %r (%s)" %
131 (cls
.__name
__, dec
, type(dec
).__name
__))
132 if not dec
.is_finite():
133 # Catches infinities and nans.
134 raise TypeError("Cannot convert %s to %s." % (dec
, cls
.__name
__))
135 sign
, digits
, exp
= dec
.as_tuple()
136 digits
= int(''.join(map(str, digits
)))
140 return cls(digits
* 10 ** exp
)
142 return cls(digits
, 10 ** -exp
)
144 def limit_denominator(self
, max_denominator
=1000000):
145 """Closest Fraction to self with denominator at most max_denominator.
147 >>> Fraction('3.141592653589793').limit_denominator(10)
149 >>> Fraction('3.141592653589793').limit_denominator(100)
151 >>> Fraction(1234, 5678).limit_denominator(10000)
155 # Algorithm notes: For any real number x, define a *best upper
156 # approximation* to x to be a rational number p/q such that:
159 # (2) if p/q > r/s >= x then s > q, for any rational r/s.
161 # Define *best lower approximation* similarly. Then it can be
162 # proved that a rational number is a best upper or lower
163 # approximation to x if, and only if, it is a convergent or
164 # semiconvergent of the (unique shortest) continued fraction
167 # To find a best rational approximation with denominator <= M,
168 # we find the best upper and lower approximations with
169 # denominator <= M and take whichever of these is closer to x.
170 # In the event of a tie, the bound with smaller denominator is
171 # chosen. If both denominators are equal (which can happen
172 # only when max_denominator == 1 and self is midway between
173 # two integers) the lower bound---i.e., the floor of self, is
176 if max_denominator
< 1:
177 raise ValueError("max_denominator should be at least 1")
178 if self
._denominator
<= max_denominator
:
179 return Fraction(self
)
181 p0
, q0
, p1
, q1
= 0, 1, 1, 0
182 n
, d
= self
._numerator
, self
._denominator
186 if q2
> max_denominator
:
188 p0
, q0
, p1
, q1
= p1
, q1
, p0
+a
*p1
, q2
191 k
= (max_denominator
-q0
)//q1
192 bound1
= Fraction(p0
+k
*p1
, q0
+k
*q1
)
193 bound2
= Fraction(p1
, q1
)
194 if abs(bound2
- self
) <= abs(bound1
-self
):
205 return a
._denominator
209 return ('Fraction(%s, %s)' % (self
._numerator
, self
._denominator
))
213 if self
._denominator
== 1:
214 return str(self
._numerator
)
216 return '%s/%s' % (self
._numerator
, self
._denominator
)
218 def _operator_fallbacks(monomorphic_operator
, fallback_operator
):
219 """Generates forward and reverse operators given a purely-rational
220 operator and a function from the operator module.
223 __op__, __rop__ = _operator_fallbacks(just_rational_op, operator.op)
225 In general, we want to implement the arithmetic operations so
226 that mixed-mode operations either call an implementation whose
227 author knew about the types of both arguments, or convert both
228 to the nearest built in type and do the operation there. In
229 Fraction, that means that we define __add__ and __radd__ as:
231 def __add__(self, other):
232 # Both types have numerators/denominator attributes,
233 # so do the operation directly
234 if isinstance(other, (int, long, Fraction)):
235 return Fraction(self.numerator * other.denominator +
236 other.numerator * self.denominator,
237 self.denominator * other.denominator)
238 # float and complex don't have those operations, but we
239 # know about those types, so special case them.
240 elif isinstance(other, float):
241 return float(self) + other
242 elif isinstance(other, complex):
243 return complex(self) + other
244 # Let the other type take over.
245 return NotImplemented
247 def __radd__(self, other):
248 # radd handles more types than add because there's
249 # nothing left to fall back to.
250 if isinstance(other, Rational):
251 return Fraction(self.numerator * other.denominator +
252 other.numerator * self.denominator,
253 self.denominator * other.denominator)
254 elif isinstance(other, Real):
255 return float(other) + float(self)
256 elif isinstance(other, Complex):
257 return complex(other) + complex(self)
258 return NotImplemented
261 There are 5 different cases for a mixed-type addition on
262 Fraction. I'll refer to all of the above code that doesn't
263 refer to Fraction, float, or complex as "boilerplate". 'r'
264 will be an instance of Fraction, which is a subtype of
265 Rational (r : Fraction <: Rational), and b : B <:
266 Complex. The first three involve 'r + b':
268 1. If B <: Fraction, int, float, or complex, we handle
269 that specially, and all is well.
270 2. If Fraction falls back to the boilerplate code, and it
271 were to return a value from __add__, we'd miss the
272 possibility that B defines a more intelligent __radd__,
273 so the boilerplate should return NotImplemented from
274 __add__. In particular, we don't handle Rational
275 here, even though we could get an exact answer, in case
276 the other type wants to do something special.
277 3. If B <: Fraction, Python tries B.__radd__ before
278 Fraction.__add__. This is ok, because it was
279 implemented with knowledge of Fraction, so it can
280 handle those instances before delegating to Real or
283 The next two situations describe 'b + r'. We assume that b
284 didn't know about Fraction in its implementation, and that it
285 uses similar boilerplate code:
287 4. If B <: Rational, then __radd_ converts both to the
288 builtin rational type (hey look, that's us) and
290 5. Otherwise, __radd__ tries to find the nearest common
291 base ABC, and fall back to its builtin type. Since this
292 class doesn't subclass a concrete type, there's no
293 implementation to fall back to, so we need to try as
294 hard as possible to return an actual value, or the user
295 will get a TypeError.
299 if isinstance(b
, (int, long, Fraction
)):
300 return monomorphic_operator(a
, b
)
301 elif isinstance(b
, float):
302 return fallback_operator(float(a
), b
)
303 elif isinstance(b
, complex):
304 return fallback_operator(complex(a
), b
)
306 return NotImplemented
307 forward
.__name
__ = '__' + fallback_operator
.__name
__ + '__'
308 forward
.__doc
__ = monomorphic_operator
.__doc
__
311 if isinstance(a
, Rational
):
313 return monomorphic_operator(a
, b
)
314 elif isinstance(a
, numbers
.Real
):
315 return fallback_operator(float(a
), float(b
))
316 elif isinstance(a
, numbers
.Complex
):
317 return fallback_operator(complex(a
), complex(b
))
319 return NotImplemented
320 reverse
.__name
__ = '__r' + fallback_operator
.__name
__ + '__'
321 reverse
.__doc
__ = monomorphic_operator
.__doc
__
323 return forward
, reverse
327 return Fraction(a
.numerator
* b
.denominator
+
328 b
.numerator
* a
.denominator
,
329 a
.denominator
* b
.denominator
)
331 __add__
, __radd__
= _operator_fallbacks(_add
, operator
.add
)
335 return Fraction(a
.numerator
* b
.denominator
-
336 b
.numerator
* a
.denominator
,
337 a
.denominator
* b
.denominator
)
339 __sub__
, __rsub__
= _operator_fallbacks(_sub
, operator
.sub
)
343 return Fraction(a
.numerator
* b
.numerator
, a
.denominator
* b
.denominator
)
345 __mul__
, __rmul__
= _operator_fallbacks(_mul
, operator
.mul
)
349 return Fraction(a
.numerator
* b
.denominator
,
350 a
.denominator
* b
.numerator
)
352 __truediv__
, __rtruediv__
= _operator_fallbacks(_div
, operator
.truediv
)
353 __div__
, __rdiv__
= _operator_fallbacks(_div
, operator
.div
)
355 def __floordiv__(a
, b
):
357 # Will be math.floor(a / b) in 3.0.
359 if isinstance(div
, Rational
):
360 # trunc(math.floor(div)) doesn't work if the rational is
361 # more precise than a float because the intermediate
362 # rounding may cross an integer boundary.
363 return div
.numerator
// div
.denominator
365 return math
.floor(div
)
367 def __rfloordiv__(b
, a
):
369 # Will be math.floor(a / b) in 3.0.
371 if isinstance(div
, Rational
):
372 # trunc(math.floor(div)) doesn't work if the rational is
373 # more precise than a float because the intermediate
374 # rounding may cross an integer boundary.
375 return div
.numerator
// div
.denominator
377 return math
.floor(div
)
392 If b is not an integer, the result will be a float or complex
393 since roots are generally irrational. If b is an integer, the
394 result will be rational.
397 if isinstance(b
, Rational
):
398 if b
.denominator
== 1:
401 return Fraction(a
._numerator
** power
,
402 a
._denominator
** power
)
404 return Fraction(a
._denominator
** -power
,
405 a
._numerator
** -power
)
407 # A fractional power will generally produce an
409 return float(a
) ** float(b
)
415 if b
._denominator
== 1 and b
._numerator
>= 0:
416 # If a is an int, keep it that way if possible.
417 return a
** b
._numerator
419 if isinstance(a
, Rational
):
420 return Fraction(a
.numerator
, a
.denominator
) ** b
422 if b
._denominator
== 1:
423 return a
** b
._numerator
428 """+a: Coerces a subclass instance to Fraction"""
429 return Fraction(a
._numerator
, a
._denominator
)
433 return Fraction(-a
._numerator
, a
._denominator
)
437 return Fraction(abs(a
._numerator
), a
._denominator
)
442 return -(-a
._numerator
// a
._denominator
)
444 return a
._numerator
// a
._denominator
449 Tricky because values that are exactly representable as a
450 float must have the same hash as that float.
453 # XXX since this method is expensive, consider caching the result
454 if self
._denominator
== 1:
455 # Get integers right.
456 return hash(self
._numerator
)
457 # Expensive check, but definitely correct.
458 if self
== float(self
):
459 return hash(float(self
))
461 # Use tuple's hash to avoid a high collision rate on
463 return hash((self
._numerator
, self
._denominator
))
467 if isinstance(b
, Rational
):
468 return (a
._numerator
== b
.numerator
and
469 a
._denominator
== b
.denominator
)
470 if isinstance(b
, numbers
.Complex
) and b
.imag
== 0:
472 if isinstance(b
, float):
473 return a
== a
.from_float(b
)
475 # XXX: If b.__eq__ is implemented like this method, it may
476 # give the wrong answer after float(a) changes a's
477 # value. Better ways of doing this are welcome.
480 def _subtractAndCompareToZero(a
, b
, op
):
481 """Helper function for comparison operators.
483 Subtracts b from a, exactly if possible, and compares the
484 result with 0 using op, in such a way that the comparison
485 won't recurse. If the difference raises a TypeError, returns
486 NotImplemented instead.
489 if isinstance(b
, numbers
.Complex
) and b
.imag
== 0:
491 if isinstance(b
, float):
494 # XXX: If b <: Real but not <: Rational, this is likely
495 # to fall back to a float. If the actual values differ by
496 # less than MIN_FLOAT, this could falsely call them equal,
497 # which would make <= inconsistent with ==. Better ways of
498 # doing this are welcome.
501 return NotImplemented
502 if isinstance(diff
, Rational
):
503 return op(diff
.numerator
, 0)
508 return a
._subtractAndCompareToZero
(b
, operator
.lt
)
512 return a
._subtractAndCompareToZero
(b
, operator
.gt
)
516 return a
._subtractAndCompareToZero
(b
, operator
.le
)
520 return a
._subtractAndCompareToZero
(b
, operator
.ge
)
524 return a
._numerator
!= 0
526 # support for pickling, copy, and deepcopy
528 def __reduce__(self
):
529 return (self
.__class
__, (str(self
),))
532 if type(self
) == Fraction
:
533 return self
# I'm immutable; therefore I am my own clone
534 return self
.__class
__(self
._numerator
, self
._denominator
)
536 def __deepcopy__(self
, memo
):
537 if type(self
) == Fraction
:
538 return self
# My components are also immutable
539 return self
.__class
__(self
._numerator
, self
._denominator
)