| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
1 parent ff9bf27 commit cd607cc
21 files changed
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -986,10 +986,13 @@ available. They are listed here in alphabetical order. | |||
| 986 | 986 | .. function:: round(x[, n]) | |
| 987 | 987 | ||
| 988 | 988 | Return the floating point value *x* rounded to *n* digits after the decimal | |
| 989 | - point. If *n* is omitted, it defaults to zero. The result is a floating point | ||
| 990 | - number. Values are rounded to the closest multiple of 10 to the power minus | ||
| 991 | - *n*; if two multiples are equally close, rounding is done away from 0 (so. for | ||
| 992 | - example, ``round(0.5)`` is ``1.0`` and ``round(-0.5)`` is ``-1.0``). | ||
| 989 | + point. If *n* is omitted, it defaults to zero. Values are rounded to the | ||
| 990 | + closest multiple of 10 to the power minus *n*; if two multiples are equally | ||
| 991 | + close, rounding is done toward the even choice (so, for example, both | ||
| 992 | + ``round(0.5)`` and ``round(-0.5)`` are ``0``, and ``round(1.5)`` is | ||
| 993 | + ``2``). Delegates to ``x.__round__(n)``. | ||
| 994 | + | ||
| 995 | + .. versionchanged:: 2.6 | ||
| 993 | 996 | ||
| 994 | 997 | ||
| 995 | 998 | .. function:: set([iterable]) | |
@@ -1132,6 +1135,14 @@ available. They are listed here in alphabetical order. | |||
| 1132 | 1135 | .. versionadded:: 2.2 | |
| 1133 | 1136 | ||
| 1134 | 1137 | ||
| 1138 | + .. function:: trunc(x) | ||
| 1139 | + | ||
| 1140 | + Return the :class:`Real` value *x* truncated to an :class:`Integral` (usually | ||
| 1141 | + a long integer). Delegates to ``x.__trunc__()``. | ||
| 1142 | + | ||
| 1143 | + .. versionadded:: 2.6 | ||
| 1144 | + | ||
| 1145 | + | ||
| 1135 | 1146 | .. function:: tuple([iterable]) | |
| 1136 | 1147 | ||
| 1137 | 1148 | Return a tuple whose items are the same and in the same order as *iterable*'s | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -26,8 +26,9 @@ Number-theoretic and representation functions: | |||
| 26 | 26 | ||
| 27 | 27 | .. function:: ceil(x) | |
| 28 | 28 | ||
| 29 | - Return the ceiling of *x* as a float, the smallest integer value greater than or | ||
| 30 | - equal to *x*. | ||
| 29 | + Return the ceiling of *x* as a float, the smallest integer value greater than | ||
| 30 | + or equal to *x*. If *x* is not a float, delegates to ``x.__ceil__()``, which | ||
| 31 | + should return an :class:`Integral` value. | ||
| 31 | 32 | ||
| 32 | 33 | ||
| 33 | 34 | .. function:: fabs(x) | |
@@ -37,8 +38,9 @@ Number-theoretic and representation functions: | |||
| 37 | 38 | ||
| 38 | 39 | .. function:: floor(x) | |
| 39 | 40 | ||
| 40 | - Return the floor of *x* as a float, the largest integer value less than or equal | ||
| 41 | - to *x*. | ||
| 41 | + Return the floor of *x* as a float, the largest integer value less than or | ||
| 42 | + equal to *x*. If *x* is not a float, delegates to ``x.__floor__()``, which | ||
| 43 | + should return an :class:`Integral` value. | ||
| 42 | 44 | ||
| 43 | 45 | ||
| 44 | 46 | .. function:: fmod(x, y) | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -0,0 +1,99 @@ | |||
| 1 | + | ||
| 2 | + :mod:`numbers` --- Numeric abstract base classes | ||
| 3 | + ================================================ | ||
| 4 | + | ||
| 5 | + .. module:: numbers | ||
| 6 | + :synopsis: Numeric abstract base classes (Complex, Real, Integral, etc.). | ||
| 7 | + | ||
| 8 | + The :mod:`numbers` module (:pep:`3141`) defines a hierarchy of numeric abstract | ||
| 9 | + base classes which progressively define more operations. These concepts also | ||
| 10 | + provide a way to distinguish exact from inexact types. None of the types defined | ||
| 11 | + in this module can be instantiated. | ||
| 12 | + | ||
| 13 | + | ||
| 14 | + .. class:: Number | ||
| 15 | + | ||
| 16 | + The root of the numeric hierarchy. If you just want to check if an argument | ||
| 17 | + *x* is a number, without caring what kind, use ``isinstance(x, Number)``. | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + Exact and inexact operations | ||
| 21 | + ---------------------------- | ||
| 22 | + | ||
| 23 | + .. class:: Exact | ||
| 24 | + | ||
| 25 | + Subclasses of this type have exact operations. | ||
| 26 | + | ||
| 27 | + As long as the result of a homogenous operation is of the same type, you can | ||
| 28 | + assume that it was computed exactly, and there are no round-off errors. Laws | ||
| 29 | + like commutativity and associativity hold. | ||
| 30 | + | ||
| 31 | + | ||
| 32 | + .. class:: Inexact | ||
| 33 | + | ||
| 34 | + Subclasses of this type have inexact operations. | ||
| 35 | + | ||
| 36 | + Given X, an instance of :class:`Inexact`, it is possible that ``(X + -X) + 3 | ||
| 37 | + == 3``, but ``X + (-X + 3) == 0``. The exact form this error takes will vary | ||
| 38 | + by type, but it's generally unsafe to compare this type for equality. | ||
| 39 | + | ||
| 40 | + | ||
| 41 | + The numeric tower | ||
| 42 | + ----------------- | ||
| 43 | + | ||
| 44 | + .. class:: Complex | ||
| 45 | + | ||
| 46 | + Subclasses of this type describe complex numbers and include the operations | ||
| 47 | + that work on the builtin :class:`complex` type. These are: conversions to | ||
| 48 | + :class:`complex` and :class:`bool`, :attr:`.real`, :attr:`.imag`, ``+``, | ||
| 49 | + ``-``, ``*``, ``/``, :func:`abs`, :meth:`conjugate`, ``==``, and ``!=``. All | ||
| 50 | + except ``-`` and ``!=`` are abstract. | ||
| 51 | + | ||
| 52 | + .. attribute:: Complex.real | ||
| 53 | + | ||
| 54 | + Abstract. Retrieves the :class:`Real` component of this number. | ||
| 55 | + | ||
| 56 | + .. attribute:: Complex.imag | ||
| 57 | + | ||
| 58 | + Abstract. Retrieves the :class:`Real` component of this number. | ||
| 59 | + | ||
| 60 | + .. method:: Complex.conjugate() | ||
| 61 | + | ||
| 62 | + Abstract. Returns the complex conjugate. For example, ``(1+3j).conjugate() == | ||
| 63 | + (1-3j)``. | ||
| 64 | + | ||
| 65 | + .. class:: Real | ||
| 66 | + | ||
| 67 | + To :class:`Complex`, :class:`Real` adds the operations that work on real | ||
| 68 | + numbers. | ||
| 69 | + | ||
| 70 | + In short, those are: a conversion to :class:`float`, :func:`trunc`, | ||
| 71 | + :func:`round`, :func:`math.floor`, :func:`math.ceil`, :func:`divmod`, ``//``, | ||
| 72 | + ``%``, ``<``, ``<=``, ``>``, and ``>=``. | ||
| 73 | + | ||
| 74 | + Real also provides defaults for :func:`complex`, :attr:`Complex.real`, | ||
| 75 | + :attr:`Complex.imag`, and :meth:`Complex.conjugate`. | ||
| 76 | + | ||
| 77 | + | ||
| 78 | + .. class:: Rational | ||
| 79 | + | ||
| 80 | + Subtypes both :class:`Real` and :class:`Exact`, and adds | ||
| 81 | + :attr:`Rational.numerator` and :attr:`Rational.denominator` properties, which | ||
| 82 | + should be in lowest terms. With these, it provides a default for | ||
| 83 | + :func:`float`. | ||
| 84 | + | ||
| 85 | + .. attribute:: Rational.numerator | ||
| 86 | + | ||
| 87 | + Abstract. | ||
| 88 | + | ||
| 89 | + .. attribute:: Rational.denominator | ||
| 90 | + | ||
| 91 | + Abstract. | ||
| 92 | + | ||
| 93 | + | ||
| 94 | + .. class:: Integral | ||
| 95 | + | ||
| 96 | + Subtypes :class:`Rational` and adds a conversion to :class:`long`, the | ||
| 97 | + 3-argument form of :func:`pow`, and the bit-string operations: ``<<``, | ||
| 98 | + ``>>``, ``&``, ``^``, ``|``, ``~``. Provides defaults for :func:`float`, | ||
| 99 | + :attr:`Rational.numerator`, and :attr:`Rational.denominator`. | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -6,16 +6,18 @@ Numeric and Mathematical Modules | |||
| 6 | 6 | ******************************** | |
| 7 | 7 | ||
| 8 | 8 | The modules described in this chapter provide numeric and math-related functions | |
| 9 | - and data types. The :mod:`math` and :mod:`cmath` contain various mathematical | ||
| 10 | - functions for floating-point and complex numbers. For users more interested in | ||
| 11 | - decimal accuracy than in speed, the :mod:`decimal` module supports exact | ||
| 12 | - representations of decimal numbers. | ||
| 9 | + and data types. The :mod:`numbers` module defines an abstract hierarchy of | ||
| 10 | + numeric types. The :mod:`math` and :mod:`cmath` modules contain various | ||
| 11 | + mathematical functions for floating-point and complex numbers. For users more | ||
| 12 | + interested in decimal accuracy than in speed, the :mod:`decimal` module supports | ||
| 13 | + exact representations of decimal numbers. | ||
| 13 | 14 | ||
| 14 | 15 | The following modules are documented in this chapter: | |
| 15 | 16 | ||
| 16 | 17 | ||
| 17 | 18 | .. toctree:: | |
| 18 | 19 | ||
| 20 | + numbers.rst | ||
| 19 | 21 | math.rst | |
| 20 | 22 | cmath.rst | |
| 21 | 23 | decimal.rst | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -270,9 +270,8 @@ numbers of mixed type use the same rule. [#]_ The constructors :func:`int`, | |||
| 270 | 270 | :func:`long`, :func:`float`, and :func:`complex` can be used to produce numbers | |
| 271 | 271 | of a specific type. | |
| 272 | 272 | ||
| 273 | - All numeric types (except complex) support the following operations, sorted by | ||
| 274 | - ascending priority (operations in the same box have the same priority; all | ||
| 275 | - numeric operations have a higher priority than comparison operations): | ||
| 273 | + All builtin numeric types support the following operations. See | ||
| 274 | + :ref:`power` and later sections for the operators' priorities. | ||
| 276 | 275 | ||
| 277 | 276 | +--------------------+---------------------------------+--------+ | |
| 278 | 277 | | Operation | Result | Notes | | |
@@ -285,7 +284,7 @@ numeric operations have a higher priority than comparison operations): | |||
| 285 | 284 | +--------------------+---------------------------------+--------+ | |
| 286 | 285 | | ``x / y`` | quotient of *x* and *y* | \(1) | | |
| 287 | 286 | +--------------------+---------------------------------+--------+ | |
| 288 | - | ``x // y`` | (floored) quotient of *x* and | \(5) | | ||
| 287 | + | ``x // y`` | (floored) quotient of *x* and | (4)(5) | | ||
| 289 | 288 | | | *y* | | | |
| 290 | 289 | +--------------------+---------------------------------+--------+ | |
| 291 | 290 | | ``x % y`` | remainder of ``x / y`` | \(4) | | |
@@ -294,7 +293,7 @@ numeric operations have a higher priority than comparison operations): | |||
| 294 | 293 | +--------------------+---------------------------------+--------+ | |
| 295 | 294 | | ``+x`` | *x* unchanged | | | |
| 296 | 295 | +--------------------+---------------------------------+--------+ | |
| 297 | - | ``abs(x)`` | absolute value or magnitude of | | | ||
| 296 | + | ``abs(x)`` | absolute value or magnitude of | \(3) | | ||
| 298 | 297 | | | *x* | | | |
| 299 | 298 | +--------------------+---------------------------------+--------+ | |
| 300 | 299 | | ``int(x)`` | *x* converted to integer | \(2) | | |
@@ -308,11 +307,11 @@ numeric operations have a higher priority than comparison operations): | |||
| 308 | 307 | | | *im* defaults to zero. | | | |
| 309 | 308 | +--------------------+---------------------------------+--------+ | |
| 310 | 309 | | ``c.conjugate()`` | conjugate of the complex number | | | |
| 311 | - | | *c* | | | ||
| 310 | + | | *c*. (Identity on real numbers) | | | ||
| 312 | 311 | +--------------------+---------------------------------+--------+ | |
| 313 | 312 | | ``divmod(x, y)`` | the pair ``(x // y, x % y)`` | (3)(4) | | |
| 314 | 313 | +--------------------+---------------------------------+--------+ | |
| 315 | - | ``pow(x, y)`` | *x* to the power *y* | | | ||
| 314 | + | ``pow(x, y)`` | *x* to the power *y* | \(3) | | ||
| 316 | 315 | +--------------------+---------------------------------+--------+ | |
| 317 | 316 | | ``x ** y`` | *x* to the power *y* | | | |
| 318 | 317 | +--------------------+---------------------------------+--------+ | |
@@ -341,9 +340,12 @@ Notes: | |||
| 341 | 340 | pair: numeric; conversions | |
| 342 | 341 | pair: C; language | |
| 343 | 342 | ||
| 344 | - Conversion from floating point to (long or plain) integer may round or truncate | ||
| 345 | - as in C; see functions :func:`floor` and :func:`ceil` in the :mod:`math` module | ||
| 346 | - for well-defined conversions. | ||
| 343 | + Conversion from floating point to (long or plain) integer may round or | ||
| 344 | + truncate as in C. | ||
| 345 | + | ||
| 346 | + .. deprecated:: 2.6 | ||
| 347 | + Instead, convert floats to long explicitly with :func:`trunc`, | ||
| 348 | + :func:`math.floor`, or :func:`math.ceil`. | ||
| 347 | 349 | ||
| 348 | 350 | (3) | |
| 349 | 351 | See :ref:`built-in-funcs` for a full description. | |
@@ -364,6 +366,22 @@ Notes: | |||
| 364 | 366 | ||
| 365 | 367 | .. versionadded:: 2.6 | |
| 366 | 368 | ||
| 369 | + All :class:`numbers.Real` types (:class:`int`, :class:`long`, and | ||
| 370 | + :class:`float`) also include the following operations: | ||
| 371 | + | ||
| 372 | + +--------------------+--------------------------------+--------+ | ||
| 373 | + | Operation | Result | Notes | | ||
| 374 | + +====================+================================+========+ | ||
| 375 | + | ``trunc(x)`` | *x* truncated to Integral | | | ||
| 376 | + +--------------------+--------------------------------+--------+ | ||
| 377 | + | ``round(x[, n])`` | *x* rounded to n digits, | | | ||
| 378 | + | | rounding half to even. If n is | | | ||
| 379 | + | | omitted, it defaults to 0. | | | ||
| 380 | + +--------------------+--------------------------------+--------+ | ||
| 381 | + | ``math.floor(x)`` | the greatest Integral <= *x* | | | ||
| 382 | + +--------------------+--------------------------------+--------+ | ||
| 383 | + | ``math.ceil(x)`` | the least Integral >= *x* | | | ||
| 384 | + +--------------------+--------------------------------+--------+ | ||
| 367 | 385 | ||
| 368 | 386 | .. XXXJH exceptions: overflow (when? what operations?) zerodivision | |
| 369 | 387 | ||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -150,7 +150,7 @@ Ellipsis | |||
| 150 | 150 | indicate the presence of the ``...`` syntax in a slice. Its truth value is | |
| 151 | 151 | true. | |
| 152 | 152 | ||
| 153 | - Numbers | ||
| 153 | + :class:`numbers.Number` | ||
| 154 | 154 | .. index:: object: numeric | |
| 155 | 155 | ||
| 156 | 156 | These are created by numeric literals and returned as results by arithmetic | |
@@ -162,7 +162,7 @@ Numbers | |||
| 162 | 162 | Python distinguishes between integers, floating point numbers, and complex | |
| 163 | 163 | numbers: | |
| 164 | 164 | ||
| 165 | - Integers | ||
| 165 | + :class:`numbers.Integral` | ||
| 166 | 166 | .. index:: object: integer | |
| 167 | 167 | ||
| 168 | 168 | These represent elements from the mathematical set of integers (positive and | |
@@ -214,7 +214,7 @@ Numbers | |||
| 214 | 214 | without causing overflow, will yield the same result in the long integer domain | |
| 215 | 215 | or when using mixed operands. | |
| 216 | 216 | ||
| 217 | - Floating point numbers | ||
| 217 | + :class:`numbers.Real` (:class:`float`) | ||
| 218 | 218 | .. index:: | |
| 219 | 219 | object: floating point | |
| 220 | 220 | pair: floating point; number | |
@@ -229,7 +229,7 @@ Numbers | |||
| 229 | 229 | overhead of using objects in Python, so there is no reason to complicate the | |
| 230 | 230 | language with two kinds of floating point numbers. | |
| 231 | 231 | ||
| 232 | - Complex numbers | ||
| 232 | + :class:`numbers.Complex` | ||
| 233 | 233 | .. index:: | |
| 234 | 234 | object: complex | |
| 235 | 235 | pair: complex; number | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
@@ -801,7 +801,8 @@ were of integer types and the second argument was negative, an exception was | |||
| 801 | 801 | raised). | |
| 802 | 802 | ||
| 803 | 803 | Raising ``0.0`` to a negative power results in a :exc:`ZeroDivisionError`. | |
| 804 | - Raising a negative number to a fractional power results in a :exc:`ValueError`. | ||
| 804 | + Raising a negative number to a fractional power results in a :class:`complex` | ||
| 805 | + number. (Since Python 2.6. In earlier versions it raised a :exc:`ValueError`.) | ||
| 805 | 806 | ||
| 806 | 807 | ||
| 807 | 808 | .. _unary: | |
| Back | FazBrowse Home | New Git URL |
0 commit comments