Mercurial > cpython-lru_cache-weakref
annotate Lib/numbers.py @ 58925:eecf1bef052d 2.6
Merged revisions 78207 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r78207 | ezio.melotti | 2010-02-17 01:26:09 +0200 (Wed, 17 Feb 2010) | 1 line
#7930: fix stripid
........
| author | Ezio Melotti <ezio.melotti@gmail.com> |
|---|---|
| date | Tue, 16 Feb 2010 23:29:44 +0000 |
| parents | 31645ead4c35 |
| children | 5c082f197ddf |
| rev | line source |
|---|---|
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
1 # Copyright 2007 Google, Inc. All Rights Reserved. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
2 # Licensed to PSF under a Contributor Agreement. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
3 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
4 """Abstract Base Classes (ABCs) for numbers, according to PEP 3141. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
5 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
6 TODO: Fill out more detailed documentation on the operators.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
7 |
|
44691
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
8 from __future__ import division |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
9 from abc import ABCMeta, abstractmethod, abstractproperty |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
10 |
|
45769
531e62416eeb
Removed Exact/Inexact after discussion with Yasskin.
Raymond Hettinger <python@rcn.com>
parents:
45320
diff
changeset
|
11 __all__ = ["Number", "Complex", "Real", "Rational", "Integral"] |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
12 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
13 class Number(object): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
14 """All numbers inherit from this class. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
15 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
16 If you just want to check if an argument x is a number, without |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
17 caring what kind, use isinstance(x, Number). |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
18 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
19 __metaclass__ = ABCMeta |
|
51568
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
20 __slots__ = () |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
21 |
|
48213
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47749
diff
changeset
|
22 # Concrete numeric types must provide their own hash implementation |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47749
diff
changeset
|
23 __hash__ = None |
|
7527da2b05e8
Issue 2235: Py3k warnings are now emitted for classes that will no longer inherit a__hash__ implementation from a parent class in Python 3.x. The standard library and test suite have been updated to not emit these warnings.
Nick Coghlan <ncoghlan@gmail.com>
parents:
47749
diff
changeset
|
24 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
25 |
|
45320
7372308eb62f
Update notes on Decimal.
Raymond Hettinger <python@rcn.com>
parents:
45293
diff
changeset
|
26 ## Notes on Decimal |
|
7372308eb62f
Update notes on Decimal.
Raymond Hettinger <python@rcn.com>
parents:
45293
diff
changeset
|
27 ## ---------------- |
|
7372308eb62f
Update notes on Decimal.
Raymond Hettinger <python@rcn.com>
parents:
45293
diff
changeset
|
28 ## Decimal has all of the methods specified by the Real abc, but it should |
|
7372308eb62f
Update notes on Decimal.
Raymond Hettinger <python@rcn.com>
parents:
45293
diff
changeset
|
29 ## not be registered as a Real because decimals do not interoperate with |
|
45769
531e62416eeb
Removed Exact/Inexact after discussion with Yasskin.
Raymond Hettinger <python@rcn.com>
parents:
45320
diff
changeset
|
30 ## binary floats (i.e. Decimal('3.14') + 2.71828 is undefined). But, |
|
531e62416eeb
Removed Exact/Inexact after discussion with Yasskin.
Raymond Hettinger <python@rcn.com>
parents:
45320
diff
changeset
|
31 ## abstract reals are expected to interoperate (i.e. R1 + R2 should be |
|
531e62416eeb
Removed Exact/Inexact after discussion with Yasskin.
Raymond Hettinger <python@rcn.com>
parents:
45320
diff
changeset
|
32 ## expected to work if R1 and R2 are both Reals). |
|
45287
808f84c566f0
Add notes on how decimal fits into the model.
Raymond Hettinger <python@rcn.com>
parents:
45233
diff
changeset
|
33 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
34 class Complex(Number): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
35 """Complex defines the operations that work on the builtin complex type. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
36 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
37 In short, those are: a conversion to complex, .real, .imag, +, -, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
38 *, /, abs(), .conjugate, ==, and !=. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
39 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
40 If it is given heterogenous arguments, and doesn't have special |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
41 knowledge about them, it should fall back to the builtin complex |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
42 type as described below. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
43 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
44 |
|
51568
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
45 __slots__ = () |
|
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
46 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
47 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
48 def __complex__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
49 """Return a builtin complex instance. Called for complex(self).""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
50 |
|
44691
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
51 # Will be __bool__ in 3.0. |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
52 def __nonzero__(self): |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
53 """True if self != 0. Called for bool(self).""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
54 return self != 0 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
55 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
56 @abstractproperty |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
57 def real(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
58 """Retrieve the real component of this number. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
59 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
60 This should subclass Real. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
61 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
62 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
63 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
64 @abstractproperty |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
65 def imag(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
66 """Retrieve the real component of this number. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
67 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
68 This should subclass Real. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
69 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
70 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
71 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
72 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
73 def __add__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
74 """self + other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
75 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
76 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
77 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
78 def __radd__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
79 """other + self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
80 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
81 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
82 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
83 def __neg__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
84 """-self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
85 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
86 |
|
44691
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
87 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
88 def __pos__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
89 """+self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
90 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
91 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
92 def __sub__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
93 """self - other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
94 return self + -other |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
95 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
96 def __rsub__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
97 """other - self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
98 return -self + other |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
99 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
100 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
101 def __mul__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
102 """self * other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
103 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
104 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
105 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
106 def __rmul__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
107 """other * self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
108 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
109 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
110 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
111 def __div__(self, other): |
|
44691
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
112 """self / other without __future__ division |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
113 |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
114 May promote to float. |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
115 """ |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
116 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
117 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
118 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
119 def __rdiv__(self, other): |
|
44691
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
120 """other / self without __future__ division""" |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
121 raise NotImplementedError |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
122 |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
123 @abstractmethod |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
124 def __truediv__(self, other): |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
125 """self / other with __future__ division. |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
126 |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
127 Should promote to float when necessary. |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
128 """ |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
129 raise NotImplementedError |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
130 |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
131 @abstractmethod |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
132 def __rtruediv__(self, other): |
|
46e731bb6a85
Add rational.Rational as an implementation of numbers.Rational with infinite
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44488
diff
changeset
|
133 """other / self with __future__ division""" |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
134 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
135 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
136 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
137 def __pow__(self, exponent): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
138 """self**exponent; should promote to float or complex when necessary.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
139 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
140 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
141 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
142 def __rpow__(self, base): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
143 """base ** self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
144 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
145 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
146 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
147 def __abs__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
148 """Returns the Real distance from 0. Called for abs(self).""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
149 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
150 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
151 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
152 def conjugate(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
153 """(x+y*i).conjugate() returns (x-y*i).""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
154 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
155 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
156 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
157 def __eq__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
158 """self == other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
159 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
160 |
|
45233
54dcf806ded6
Oops! 2.6's Rational.__ne__ didn't work.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45070
diff
changeset
|
161 def __ne__(self, other): |
|
54dcf806ded6
Oops! 2.6's Rational.__ne__ didn't work.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45070
diff
changeset
|
162 """self != other""" |
|
54dcf806ded6
Oops! 2.6's Rational.__ne__ didn't work.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45070
diff
changeset
|
163 # The default __ne__ doesn't negate __eq__ until 3.0. |
|
54dcf806ded6
Oops! 2.6's Rational.__ne__ didn't work.
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
45070
diff
changeset
|
164 return not (self == other) |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
165 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
166 Complex.register(complex) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
167 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
168 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
169 class Real(Complex): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
170 """To Complex, Real adds the operations that work on real numbers. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
171 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
172 In short, those are: a conversion to float, trunc(), divmod, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
173 %, <, <=, >, and >=. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
174 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
175 Real also provides defaults for the derived operations. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
176 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
177 |
|
51568
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
178 __slots__ = () |
|
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
179 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
180 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
181 def __float__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
182 """Any Real can be converted to a native float object. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
183 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
184 Called for float(self).""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
185 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
186 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
187 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
188 def __trunc__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
189 """trunc(self): Truncates self to an Integral. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
190 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
191 Returns an Integral i such that: |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
192 * i>0 iff self>0; |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
193 * abs(i) <= abs(self); |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
194 * for any Integral j satisfying the first two conditions, |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
195 abs(i) >= abs(j) [i.e. i has "maximal" abs among those]. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
196 i.e. "truncate towards 0". |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
197 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
198 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
199 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
200 def __divmod__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
201 """divmod(self, other): The pair (self // other, self % other). |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
202 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
203 Sometimes this can be computed faster than the pair of |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
204 operations. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
205 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
206 return (self // other, self % other) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
207 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
208 def __rdivmod__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
209 """divmod(other, self): The pair (self // other, self % other). |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
210 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
211 Sometimes this can be computed faster than the pair of |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
212 operations. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
213 """ |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
214 return (other // self, other % self) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
215 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
216 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
217 def __floordiv__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
218 """self // other: The floor() of self/other.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
219 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
220 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
221 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
222 def __rfloordiv__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
223 """other // self: The floor() of other/self.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
224 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
225 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
226 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
227 def __mod__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
228 """self % other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
229 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
230 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
231 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
232 def __rmod__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
233 """other % self""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
234 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
235 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
236 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
237 def __lt__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
238 """self < other |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
239 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
240 < on Reals defines a total ordering, except perhaps for NaN.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
241 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
242 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
243 @abstractmethod |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
244 def __le__(self, other): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
245 """self <= other""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
246 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
247 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
248 # Concrete implementations of Complex abstract methods. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
249 def __complex__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
250 """complex(self) == complex(float(self), 0)""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
251 return complex(float(self)) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
252 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
253 @property |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
254 def real(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
255 """Real numbers are their real component.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
256 return +self |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
257 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
258 @property |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
259 def imag(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
260 """Real numbers have no imaginary component.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
261 return 0 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
262 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
263 def conjugate(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
264 """Conjugate is a no-op for Reals.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
265 return +self |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
266 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
267 Real.register(float) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
268 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
269 |
|
45770
0d10f193b9be
Zap one more use of Exact/Inexact.
Raymond Hettinger <python@rcn.com>
parents:
45769
diff
changeset
|
270 class Rational(Real): |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
271 """.numerator and .denominator should be in lowest terms.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
272 |
|
51568
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
273 __slots__ = () |
|
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
274 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
275 @abstractproperty |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
276 def numerator(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
277 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
278 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
279 @abstractproperty |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
280 def denominator(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
281 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
282 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
283 # Concrete implementation of Real's conversion to float. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
284 def __float__(self): |
|
45070
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
285 """float(self) = self.numerator / self.denominator |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
286 |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
287 It's important that this conversion use the integer's "true" |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
288 division rather than casting one side to float before dividing |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
289 so that ratios of huge integers convert without overflowing. |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
290 |
|
03e4a36a56bb
Added more documentation on how mixed-mode arithmetic should be implemented. I
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
44691
diff
changeset
|
291 """ |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
292 return self.numerator / self.denominator |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
293 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
294 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
295 class Integral(Rational): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
296 """Integral adds a conversion to long and the bit-string operations.""" |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
297 |
|
51568
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
298 __slots__ = () |
|
31645ead4c35
Merged revisions 68813,69546 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
48213
diff
changeset
|
299 |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
300 @abstractmethod |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
301 def __long__(self): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
302 """long(self)""" |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
303 raise NotImplementedError |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
304 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
305 def __index__(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
306 """index(self)""" |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
307 return long(self) |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
308 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
309 @abstractmethod |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
310 def __pow__(self, exponent, modulus=None): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
311 """self ** exponent % modulus, but maybe faster. |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
312 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
313 Accept the modulus argument if you want to support the |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
314 3-argument version of pow(). Raise a TypeError if exponent < 0 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
315 or any argument isn't Integral. Otherwise, just implement the |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
316 2-argument version described in Complex. |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
317 """ |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
318 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
319 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
320 @abstractmethod |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
321 def __lshift__(self, other): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
322 """self << other""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
323 raise NotImplementedError |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
324 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
325 @abstractmethod |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
326 def __rlshift__(self, other): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
327 """other << self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
328 raise NotImplementedError |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
329 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
330 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
331 def __rshift__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
332 """self >> other""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
333 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
334 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
335 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
336 def __rrshift__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
337 """other >> self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
338 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
339 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
340 @abstractmethod |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
341 def __and__(self, other): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
342 """self & other""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
343 raise NotImplementedError |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
344 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
345 @abstractmethod |
|
47572
013be6320b82
Mini-PEP: Simplifying numbers.py
Raymond Hettinger <python@rcn.com>
parents:
45770
diff
changeset
|
346 def __rand__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
347 """other & self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
348 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
349 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
350 @abstractmethod |
|
47572
013be6320b82
Mini-PEP: Simplifying numbers.py
Raymond Hettinger <python@rcn.com>
parents:
45770
diff
changeset
|
351 def __xor__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
352 """self ^ other""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
353 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
354 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
355 @abstractmethod |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
356 def __rxor__(self, other): |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
357 """other ^ self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
358 raise NotImplementedError |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
359 |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
360 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
361 def __or__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
362 """self | other""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
363 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
364 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
365 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
366 def __ror__(self, other): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
367 """other | self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
368 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
369 |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
370 @abstractmethod |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
371 def __invert__(self): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
372 """~self""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
373 raise NotImplementedError |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
374 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
375 # Concrete implementations of Rational and Real abstract methods. |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
376 def __float__(self): |
|
47749
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
377 """float(self) == float(long(self))""" |
|
40a75913f04a
Roll back Raymond's -r64098 while we think of something better.
Guido van Rossum <guido@python.org>
parents:
47572
diff
changeset
|
378 return float(long(self)) |
|
44432
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
379 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
380 @property |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
381 def numerator(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
382 """Integers are their own numerators.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
383 return +self |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
384 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
385 @property |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
386 def denominator(self): |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
387 """Integers have a denominator of 1.""" |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
388 return 1 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
389 |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
390 Integral.register(int) |
|
4f9723d9ca32
Backport PEP 3141 from the py3k branch to the trunk. This includes r50877 (just
Jeffrey Yasskin <jyasskin@gmail.com>
parents:
diff
changeset
|
391 Integral.register(long) |
