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