xref: /aosp_15_r20/prebuilts/build-tools/common/py3-stdlib/numbers.py (revision cda5da8d549138a6648c5ee6d7a49cf8f4a657be)
1*cda5da8dSAndroid Build Coastguard Worker# Copyright 2007 Google, Inc. All Rights Reserved.
2*cda5da8dSAndroid Build Coastguard Worker# Licensed to PSF under a Contributor Agreement.
3*cda5da8dSAndroid Build Coastguard Worker
4*cda5da8dSAndroid Build Coastguard Worker"""Abstract Base Classes (ABCs) for numbers, according to PEP 3141.
5*cda5da8dSAndroid Build Coastguard Worker
6*cda5da8dSAndroid Build Coastguard WorkerTODO: Fill out more detailed documentation on the operators."""
7*cda5da8dSAndroid Build Coastguard Worker
8*cda5da8dSAndroid Build Coastguard Workerfrom abc import ABCMeta, abstractmethod
9*cda5da8dSAndroid Build Coastguard Worker
10*cda5da8dSAndroid Build Coastguard Worker__all__ = ["Number", "Complex", "Real", "Rational", "Integral"]
11*cda5da8dSAndroid Build Coastguard Worker
12*cda5da8dSAndroid Build Coastguard Workerclass Number(metaclass=ABCMeta):
13*cda5da8dSAndroid Build Coastguard Worker    """All numbers inherit from this class.
14*cda5da8dSAndroid Build Coastguard Worker
15*cda5da8dSAndroid Build Coastguard Worker    If you just want to check if an argument x is a number, without
16*cda5da8dSAndroid Build Coastguard Worker    caring what kind, use isinstance(x, Number).
17*cda5da8dSAndroid Build Coastguard Worker    """
18*cda5da8dSAndroid Build Coastguard Worker    __slots__ = ()
19*cda5da8dSAndroid Build Coastguard Worker
20*cda5da8dSAndroid Build Coastguard Worker    # Concrete numeric types must provide their own hash implementation
21*cda5da8dSAndroid Build Coastguard Worker    __hash__ = None
22*cda5da8dSAndroid Build Coastguard Worker
23*cda5da8dSAndroid Build Coastguard Worker
24*cda5da8dSAndroid Build Coastguard Worker## Notes on Decimal
25*cda5da8dSAndroid Build Coastguard Worker## ----------------
26*cda5da8dSAndroid Build Coastguard Worker## Decimal has all of the methods specified by the Real abc, but it should
27*cda5da8dSAndroid Build Coastguard Worker## not be registered as a Real because decimals do not interoperate with
28*cda5da8dSAndroid Build Coastguard Worker## binary floats (i.e.  Decimal('3.14') + 2.71828 is undefined).  But,
29*cda5da8dSAndroid Build Coastguard Worker## abstract reals are expected to interoperate (i.e. R1 + R2 should be
30*cda5da8dSAndroid Build Coastguard Worker## expected to work if R1 and R2 are both Reals).
31*cda5da8dSAndroid Build Coastguard Worker
32*cda5da8dSAndroid Build Coastguard Workerclass Complex(Number):
33*cda5da8dSAndroid Build Coastguard Worker    """Complex defines the operations that work on the builtin complex type.
34*cda5da8dSAndroid Build Coastguard Worker
35*cda5da8dSAndroid Build Coastguard Worker    In short, those are: a conversion to complex, .real, .imag, +, -,
36*cda5da8dSAndroid Build Coastguard Worker    *, /, **, abs(), .conjugate, ==, and !=.
37*cda5da8dSAndroid Build Coastguard Worker
38*cda5da8dSAndroid Build Coastguard Worker    If it is given heterogeneous arguments, and doesn't have special
39*cda5da8dSAndroid Build Coastguard Worker    knowledge about them, it should fall back to the builtin complex
40*cda5da8dSAndroid Build Coastguard Worker    type as described below.
41*cda5da8dSAndroid Build Coastguard Worker    """
42*cda5da8dSAndroid Build Coastguard Worker
43*cda5da8dSAndroid Build Coastguard Worker    __slots__ = ()
44*cda5da8dSAndroid Build Coastguard Worker
45*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
46*cda5da8dSAndroid Build Coastguard Worker    def __complex__(self):
47*cda5da8dSAndroid Build Coastguard Worker        """Return a builtin complex instance. Called for complex(self)."""
48*cda5da8dSAndroid Build Coastguard Worker
49*cda5da8dSAndroid Build Coastguard Worker    def __bool__(self):
50*cda5da8dSAndroid Build Coastguard Worker        """True if self != 0. Called for bool(self)."""
51*cda5da8dSAndroid Build Coastguard Worker        return self != 0
52*cda5da8dSAndroid Build Coastguard Worker
53*cda5da8dSAndroid Build Coastguard Worker    @property
54*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
55*cda5da8dSAndroid Build Coastguard Worker    def real(self):
56*cda5da8dSAndroid Build Coastguard Worker        """Retrieve the real component of this number.
57*cda5da8dSAndroid Build Coastguard Worker
58*cda5da8dSAndroid Build Coastguard Worker        This should subclass Real.
59*cda5da8dSAndroid Build Coastguard Worker        """
60*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
61*cda5da8dSAndroid Build Coastguard Worker
62*cda5da8dSAndroid Build Coastguard Worker    @property
63*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
64*cda5da8dSAndroid Build Coastguard Worker    def imag(self):
65*cda5da8dSAndroid Build Coastguard Worker        """Retrieve the imaginary component of this number.
66*cda5da8dSAndroid Build Coastguard Worker
67*cda5da8dSAndroid Build Coastguard Worker        This should subclass Real.
68*cda5da8dSAndroid Build Coastguard Worker        """
69*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
70*cda5da8dSAndroid Build Coastguard Worker
71*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
72*cda5da8dSAndroid Build Coastguard Worker    def __add__(self, other):
73*cda5da8dSAndroid Build Coastguard Worker        """self + other"""
74*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
75*cda5da8dSAndroid Build Coastguard Worker
76*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
77*cda5da8dSAndroid Build Coastguard Worker    def __radd__(self, other):
78*cda5da8dSAndroid Build Coastguard Worker        """other + self"""
79*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
80*cda5da8dSAndroid Build Coastguard Worker
81*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
82*cda5da8dSAndroid Build Coastguard Worker    def __neg__(self):
83*cda5da8dSAndroid Build Coastguard Worker        """-self"""
84*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
85*cda5da8dSAndroid Build Coastguard Worker
86*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
87*cda5da8dSAndroid Build Coastguard Worker    def __pos__(self):
88*cda5da8dSAndroid Build Coastguard Worker        """+self"""
89*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
90*cda5da8dSAndroid Build Coastguard Worker
91*cda5da8dSAndroid Build Coastguard Worker    def __sub__(self, other):
92*cda5da8dSAndroid Build Coastguard Worker        """self - other"""
93*cda5da8dSAndroid Build Coastguard Worker        return self + -other
94*cda5da8dSAndroid Build Coastguard Worker
95*cda5da8dSAndroid Build Coastguard Worker    def __rsub__(self, other):
96*cda5da8dSAndroid Build Coastguard Worker        """other - self"""
97*cda5da8dSAndroid Build Coastguard Worker        return -self + other
98*cda5da8dSAndroid Build Coastguard Worker
99*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
100*cda5da8dSAndroid Build Coastguard Worker    def __mul__(self, other):
101*cda5da8dSAndroid Build Coastguard Worker        """self * other"""
102*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
103*cda5da8dSAndroid Build Coastguard Worker
104*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
105*cda5da8dSAndroid Build Coastguard Worker    def __rmul__(self, other):
106*cda5da8dSAndroid Build Coastguard Worker        """other * self"""
107*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
108*cda5da8dSAndroid Build Coastguard Worker
109*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
110*cda5da8dSAndroid Build Coastguard Worker    def __truediv__(self, other):
111*cda5da8dSAndroid Build Coastguard Worker        """self / other: Should promote to float when necessary."""
112*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
113*cda5da8dSAndroid Build Coastguard Worker
114*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
115*cda5da8dSAndroid Build Coastguard Worker    def __rtruediv__(self, other):
116*cda5da8dSAndroid Build Coastguard Worker        """other / self"""
117*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
118*cda5da8dSAndroid Build Coastguard Worker
119*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
120*cda5da8dSAndroid Build Coastguard Worker    def __pow__(self, exponent):
121*cda5da8dSAndroid Build Coastguard Worker        """self**exponent; should promote to float or complex when necessary."""
122*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
123*cda5da8dSAndroid Build Coastguard Worker
124*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
125*cda5da8dSAndroid Build Coastguard Worker    def __rpow__(self, base):
126*cda5da8dSAndroid Build Coastguard Worker        """base ** self"""
127*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
128*cda5da8dSAndroid Build Coastguard Worker
129*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
130*cda5da8dSAndroid Build Coastguard Worker    def __abs__(self):
131*cda5da8dSAndroid Build Coastguard Worker        """Returns the Real distance from 0. Called for abs(self)."""
132*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
133*cda5da8dSAndroid Build Coastguard Worker
134*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
135*cda5da8dSAndroid Build Coastguard Worker    def conjugate(self):
136*cda5da8dSAndroid Build Coastguard Worker        """(x+y*i).conjugate() returns (x-y*i)."""
137*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
138*cda5da8dSAndroid Build Coastguard Worker
139*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
140*cda5da8dSAndroid Build Coastguard Worker    def __eq__(self, other):
141*cda5da8dSAndroid Build Coastguard Worker        """self == other"""
142*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
143*cda5da8dSAndroid Build Coastguard Worker
144*cda5da8dSAndroid Build Coastguard WorkerComplex.register(complex)
145*cda5da8dSAndroid Build Coastguard Worker
146*cda5da8dSAndroid Build Coastguard Worker
147*cda5da8dSAndroid Build Coastguard Workerclass Real(Complex):
148*cda5da8dSAndroid Build Coastguard Worker    """To Complex, Real adds the operations that work on real numbers.
149*cda5da8dSAndroid Build Coastguard Worker
150*cda5da8dSAndroid Build Coastguard Worker    In short, those are: a conversion to float, trunc(), divmod,
151*cda5da8dSAndroid Build Coastguard Worker    %, <, <=, >, and >=.
152*cda5da8dSAndroid Build Coastguard Worker
153*cda5da8dSAndroid Build Coastguard Worker    Real also provides defaults for the derived operations.
154*cda5da8dSAndroid Build Coastguard Worker    """
155*cda5da8dSAndroid Build Coastguard Worker
156*cda5da8dSAndroid Build Coastguard Worker    __slots__ = ()
157*cda5da8dSAndroid Build Coastguard Worker
158*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
159*cda5da8dSAndroid Build Coastguard Worker    def __float__(self):
160*cda5da8dSAndroid Build Coastguard Worker        """Any Real can be converted to a native float object.
161*cda5da8dSAndroid Build Coastguard Worker
162*cda5da8dSAndroid Build Coastguard Worker        Called for float(self)."""
163*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
164*cda5da8dSAndroid Build Coastguard Worker
165*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
166*cda5da8dSAndroid Build Coastguard Worker    def __trunc__(self):
167*cda5da8dSAndroid Build Coastguard Worker        """trunc(self): Truncates self to an Integral.
168*cda5da8dSAndroid Build Coastguard Worker
169*cda5da8dSAndroid Build Coastguard Worker        Returns an Integral i such that:
170*cda5da8dSAndroid Build Coastguard Worker          * i>0 iff self>0;
171*cda5da8dSAndroid Build Coastguard Worker          * abs(i) <= abs(self);
172*cda5da8dSAndroid Build Coastguard Worker          * for any Integral j satisfying the first two conditions,
173*cda5da8dSAndroid Build Coastguard Worker            abs(i) >= abs(j) [i.e. i has "maximal" abs among those].
174*cda5da8dSAndroid Build Coastguard Worker        i.e. "truncate towards 0".
175*cda5da8dSAndroid Build Coastguard Worker        """
176*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
177*cda5da8dSAndroid Build Coastguard Worker
178*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
179*cda5da8dSAndroid Build Coastguard Worker    def __floor__(self):
180*cda5da8dSAndroid Build Coastguard Worker        """Finds the greatest Integral <= self."""
181*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
182*cda5da8dSAndroid Build Coastguard Worker
183*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
184*cda5da8dSAndroid Build Coastguard Worker    def __ceil__(self):
185*cda5da8dSAndroid Build Coastguard Worker        """Finds the least Integral >= self."""
186*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
187*cda5da8dSAndroid Build Coastguard Worker
188*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
189*cda5da8dSAndroid Build Coastguard Worker    def __round__(self, ndigits=None):
190*cda5da8dSAndroid Build Coastguard Worker        """Rounds self to ndigits decimal places, defaulting to 0.
191*cda5da8dSAndroid Build Coastguard Worker
192*cda5da8dSAndroid Build Coastguard Worker        If ndigits is omitted or None, returns an Integral, otherwise
193*cda5da8dSAndroid Build Coastguard Worker        returns a Real. Rounds half toward even.
194*cda5da8dSAndroid Build Coastguard Worker        """
195*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
196*cda5da8dSAndroid Build Coastguard Worker
197*cda5da8dSAndroid Build Coastguard Worker    def __divmod__(self, other):
198*cda5da8dSAndroid Build Coastguard Worker        """divmod(self, other): The pair (self // other, self % other).
199*cda5da8dSAndroid Build Coastguard Worker
200*cda5da8dSAndroid Build Coastguard Worker        Sometimes this can be computed faster than the pair of
201*cda5da8dSAndroid Build Coastguard Worker        operations.
202*cda5da8dSAndroid Build Coastguard Worker        """
203*cda5da8dSAndroid Build Coastguard Worker        return (self // other, self % other)
204*cda5da8dSAndroid Build Coastguard Worker
205*cda5da8dSAndroid Build Coastguard Worker    def __rdivmod__(self, other):
206*cda5da8dSAndroid Build Coastguard Worker        """divmod(other, self): The pair (self // other, self % other).
207*cda5da8dSAndroid Build Coastguard Worker
208*cda5da8dSAndroid Build Coastguard Worker        Sometimes this can be computed faster than the pair of
209*cda5da8dSAndroid Build Coastguard Worker        operations.
210*cda5da8dSAndroid Build Coastguard Worker        """
211*cda5da8dSAndroid Build Coastguard Worker        return (other // self, other % self)
212*cda5da8dSAndroid Build Coastguard Worker
213*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
214*cda5da8dSAndroid Build Coastguard Worker    def __floordiv__(self, other):
215*cda5da8dSAndroid Build Coastguard Worker        """self // other: The floor() of self/other."""
216*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
217*cda5da8dSAndroid Build Coastguard Worker
218*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
219*cda5da8dSAndroid Build Coastguard Worker    def __rfloordiv__(self, other):
220*cda5da8dSAndroid Build Coastguard Worker        """other // self: The floor() of other/self."""
221*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
222*cda5da8dSAndroid Build Coastguard Worker
223*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
224*cda5da8dSAndroid Build Coastguard Worker    def __mod__(self, other):
225*cda5da8dSAndroid Build Coastguard Worker        """self % other"""
226*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
227*cda5da8dSAndroid Build Coastguard Worker
228*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
229*cda5da8dSAndroid Build Coastguard Worker    def __rmod__(self, other):
230*cda5da8dSAndroid Build Coastguard Worker        """other % self"""
231*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
232*cda5da8dSAndroid Build Coastguard Worker
233*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
234*cda5da8dSAndroid Build Coastguard Worker    def __lt__(self, other):
235*cda5da8dSAndroid Build Coastguard Worker        """self < other
236*cda5da8dSAndroid Build Coastguard Worker
237*cda5da8dSAndroid Build Coastguard Worker        < on Reals defines a total ordering, except perhaps for NaN."""
238*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
239*cda5da8dSAndroid Build Coastguard Worker
240*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
241*cda5da8dSAndroid Build Coastguard Worker    def __le__(self, other):
242*cda5da8dSAndroid Build Coastguard Worker        """self <= other"""
243*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
244*cda5da8dSAndroid Build Coastguard Worker
245*cda5da8dSAndroid Build Coastguard Worker    # Concrete implementations of Complex abstract methods.
246*cda5da8dSAndroid Build Coastguard Worker    def __complex__(self):
247*cda5da8dSAndroid Build Coastguard Worker        """complex(self) == complex(float(self), 0)"""
248*cda5da8dSAndroid Build Coastguard Worker        return complex(float(self))
249*cda5da8dSAndroid Build Coastguard Worker
250*cda5da8dSAndroid Build Coastguard Worker    @property
251*cda5da8dSAndroid Build Coastguard Worker    def real(self):
252*cda5da8dSAndroid Build Coastguard Worker        """Real numbers are their real component."""
253*cda5da8dSAndroid Build Coastguard Worker        return +self
254*cda5da8dSAndroid Build Coastguard Worker
255*cda5da8dSAndroid Build Coastguard Worker    @property
256*cda5da8dSAndroid Build Coastguard Worker    def imag(self):
257*cda5da8dSAndroid Build Coastguard Worker        """Real numbers have no imaginary component."""
258*cda5da8dSAndroid Build Coastguard Worker        return 0
259*cda5da8dSAndroid Build Coastguard Worker
260*cda5da8dSAndroid Build Coastguard Worker    def conjugate(self):
261*cda5da8dSAndroid Build Coastguard Worker        """Conjugate is a no-op for Reals."""
262*cda5da8dSAndroid Build Coastguard Worker        return +self
263*cda5da8dSAndroid Build Coastguard Worker
264*cda5da8dSAndroid Build Coastguard WorkerReal.register(float)
265*cda5da8dSAndroid Build Coastguard Worker
266*cda5da8dSAndroid Build Coastguard Worker
267*cda5da8dSAndroid Build Coastguard Workerclass Rational(Real):
268*cda5da8dSAndroid Build Coastguard Worker    """.numerator and .denominator should be in lowest terms."""
269*cda5da8dSAndroid Build Coastguard Worker
270*cda5da8dSAndroid Build Coastguard Worker    __slots__ = ()
271*cda5da8dSAndroid Build Coastguard Worker
272*cda5da8dSAndroid Build Coastguard Worker    @property
273*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
274*cda5da8dSAndroid Build Coastguard Worker    def numerator(self):
275*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
276*cda5da8dSAndroid Build Coastguard Worker
277*cda5da8dSAndroid Build Coastguard Worker    @property
278*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
279*cda5da8dSAndroid Build Coastguard Worker    def denominator(self):
280*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
281*cda5da8dSAndroid Build Coastguard Worker
282*cda5da8dSAndroid Build Coastguard Worker    # Concrete implementation of Real's conversion to float.
283*cda5da8dSAndroid Build Coastguard Worker    def __float__(self):
284*cda5da8dSAndroid Build Coastguard Worker        """float(self) = self.numerator / self.denominator
285*cda5da8dSAndroid Build Coastguard Worker
286*cda5da8dSAndroid Build Coastguard Worker        It's important that this conversion use the integer's "true"
287*cda5da8dSAndroid Build Coastguard Worker        division rather than casting one side to float before dividing
288*cda5da8dSAndroid Build Coastguard Worker        so that ratios of huge integers convert without overflowing.
289*cda5da8dSAndroid Build Coastguard Worker
290*cda5da8dSAndroid Build Coastguard Worker        """
291*cda5da8dSAndroid Build Coastguard Worker        return int(self.numerator) / int(self.denominator)
292*cda5da8dSAndroid Build Coastguard Worker
293*cda5da8dSAndroid Build Coastguard Worker
294*cda5da8dSAndroid Build Coastguard Workerclass Integral(Rational):
295*cda5da8dSAndroid Build Coastguard Worker    """Integral adds methods that work on integral numbers.
296*cda5da8dSAndroid Build Coastguard Worker
297*cda5da8dSAndroid Build Coastguard Worker    In short, these are conversion to int, pow with modulus, and the
298*cda5da8dSAndroid Build Coastguard Worker    bit-string operations.
299*cda5da8dSAndroid Build Coastguard Worker    """
300*cda5da8dSAndroid Build Coastguard Worker
301*cda5da8dSAndroid Build Coastguard Worker    __slots__ = ()
302*cda5da8dSAndroid Build Coastguard Worker
303*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
304*cda5da8dSAndroid Build Coastguard Worker    def __int__(self):
305*cda5da8dSAndroid Build Coastguard Worker        """int(self)"""
306*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
307*cda5da8dSAndroid Build Coastguard Worker
308*cda5da8dSAndroid Build Coastguard Worker    def __index__(self):
309*cda5da8dSAndroid Build Coastguard Worker        """Called whenever an index is needed, such as in slicing"""
310*cda5da8dSAndroid Build Coastguard Worker        return int(self)
311*cda5da8dSAndroid Build Coastguard Worker
312*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
313*cda5da8dSAndroid Build Coastguard Worker    def __pow__(self, exponent, modulus=None):
314*cda5da8dSAndroid Build Coastguard Worker        """self ** exponent % modulus, but maybe faster.
315*cda5da8dSAndroid Build Coastguard Worker
316*cda5da8dSAndroid Build Coastguard Worker        Accept the modulus argument if you want to support the
317*cda5da8dSAndroid Build Coastguard Worker        3-argument version of pow(). Raise a TypeError if exponent < 0
318*cda5da8dSAndroid Build Coastguard Worker        or any argument isn't Integral. Otherwise, just implement the
319*cda5da8dSAndroid Build Coastguard Worker        2-argument version described in Complex.
320*cda5da8dSAndroid Build Coastguard Worker        """
321*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
322*cda5da8dSAndroid Build Coastguard Worker
323*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
324*cda5da8dSAndroid Build Coastguard Worker    def __lshift__(self, other):
325*cda5da8dSAndroid Build Coastguard Worker        """self << other"""
326*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
327*cda5da8dSAndroid Build Coastguard Worker
328*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
329*cda5da8dSAndroid Build Coastguard Worker    def __rlshift__(self, other):
330*cda5da8dSAndroid Build Coastguard Worker        """other << self"""
331*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
332*cda5da8dSAndroid Build Coastguard Worker
333*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
334*cda5da8dSAndroid Build Coastguard Worker    def __rshift__(self, other):
335*cda5da8dSAndroid Build Coastguard Worker        """self >> other"""
336*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
337*cda5da8dSAndroid Build Coastguard Worker
338*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
339*cda5da8dSAndroid Build Coastguard Worker    def __rrshift__(self, other):
340*cda5da8dSAndroid Build Coastguard Worker        """other >> self"""
341*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
342*cda5da8dSAndroid Build Coastguard Worker
343*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
344*cda5da8dSAndroid Build Coastguard Worker    def __and__(self, other):
345*cda5da8dSAndroid Build Coastguard Worker        """self & other"""
346*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
347*cda5da8dSAndroid Build Coastguard Worker
348*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
349*cda5da8dSAndroid Build Coastguard Worker    def __rand__(self, other):
350*cda5da8dSAndroid Build Coastguard Worker        """other & self"""
351*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
352*cda5da8dSAndroid Build Coastguard Worker
353*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
354*cda5da8dSAndroid Build Coastguard Worker    def __xor__(self, other):
355*cda5da8dSAndroid Build Coastguard Worker        """self ^ other"""
356*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
357*cda5da8dSAndroid Build Coastguard Worker
358*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
359*cda5da8dSAndroid Build Coastguard Worker    def __rxor__(self, other):
360*cda5da8dSAndroid Build Coastguard Worker        """other ^ self"""
361*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
362*cda5da8dSAndroid Build Coastguard Worker
363*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
364*cda5da8dSAndroid Build Coastguard Worker    def __or__(self, other):
365*cda5da8dSAndroid Build Coastguard Worker        """self | other"""
366*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
367*cda5da8dSAndroid Build Coastguard Worker
368*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
369*cda5da8dSAndroid Build Coastguard Worker    def __ror__(self, other):
370*cda5da8dSAndroid Build Coastguard Worker        """other | self"""
371*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
372*cda5da8dSAndroid Build Coastguard Worker
373*cda5da8dSAndroid Build Coastguard Worker    @abstractmethod
374*cda5da8dSAndroid Build Coastguard Worker    def __invert__(self):
375*cda5da8dSAndroid Build Coastguard Worker        """~self"""
376*cda5da8dSAndroid Build Coastguard Worker        raise NotImplementedError
377*cda5da8dSAndroid Build Coastguard Worker
378*cda5da8dSAndroid Build Coastguard Worker    # Concrete implementations of Rational and Real abstract methods.
379*cda5da8dSAndroid Build Coastguard Worker    def __float__(self):
380*cda5da8dSAndroid Build Coastguard Worker        """float(self) == float(int(self))"""
381*cda5da8dSAndroid Build Coastguard Worker        return float(int(self))
382*cda5da8dSAndroid Build Coastguard Worker
383*cda5da8dSAndroid Build Coastguard Worker    @property
384*cda5da8dSAndroid Build Coastguard Worker    def numerator(self):
385*cda5da8dSAndroid Build Coastguard Worker        """Integers are their own numerators."""
386*cda5da8dSAndroid Build Coastguard Worker        return +self
387*cda5da8dSAndroid Build Coastguard Worker
388*cda5da8dSAndroid Build Coastguard Worker    @property
389*cda5da8dSAndroid Build Coastguard Worker    def denominator(self):
390*cda5da8dSAndroid Build Coastguard Worker        """Integers have a denominator of 1."""
391*cda5da8dSAndroid Build Coastguard Worker        return 1
392*cda5da8dSAndroid Build Coastguard Worker
393*cda5da8dSAndroid Build Coastguard WorkerIntegral.register(int)
394