ยปCore Development>Code coverage>Lib/numbers.py

Python code coverage for Lib/numbers.py

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