Kaydet (Commit) e9135ba7 authored tarafından Georg Brandl's avatar Georg Brandl

#2147: PEP 237 changes to overflow behavior.

üst 6f95ae55
...@@ -254,11 +254,10 @@ The following exceptions are the exceptions that are actually raised. ...@@ -254,11 +254,10 @@ The following exceptions are the exceptions that are actually raised.
Raised when the result of an arithmetic operation is too large to be Raised when the result of an arithmetic operation is too large to be
represented. This cannot occur for long integers (which would rather raise represented. This cannot occur for long integers (which would rather raise
:exc:`MemoryError` than give up). Because of the lack of standardization of :exc:`MemoryError` than give up) and for most operations with plain integers,
floating point exception handling in C, most floating point operations also which return a long integer instead. Because of the lack of standardization
aren't checked. For plain integers, all operations that can overflow are of floating point exception handling in C, most floating point operations
checked except left shift, where typical applications prefer to drop bits than also aren't checked.
raise an exception.
.. exception:: ReferenceError .. exception:: ReferenceError
......
...@@ -406,8 +406,7 @@ The priorities of the binary bitwise operations are all lower than the numeric ...@@ -406,8 +406,7 @@ The priorities of the binary bitwise operations are all lower than the numeric
operations and higher than the comparisons; the unary operation ``~`` has the operations and higher than the comparisons; the unary operation ``~`` has the
same priority as the other unary numeric operations (``+`` and ``-``). same priority as the other unary numeric operations (``+`` and ``-``).
This table lists the bit-string operations sorted in ascending priority This table lists the bit-string operations sorted in ascending priority:
(operations in the same box have the same priority):
+------------+--------------------------------+----------+ +------------+--------------------------------+----------+
| Operation | Result | Notes | | Operation | Result | Notes |
...@@ -440,12 +439,11 @@ Notes: ...@@ -440,12 +439,11 @@ Notes:
Negative shift counts are illegal and cause a :exc:`ValueError` to be raised. Negative shift counts are illegal and cause a :exc:`ValueError` to be raised.
(2) (2)
A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)`` A left shift by *n* bits is equivalent to multiplication by ``pow(2, n)``. A
without overflow check. long integer is returned if the result exceeds the range of plain integers.
(3) (3)
A right shift by *n* bits is equivalent to division by ``pow(2, n)`` without A right shift by *n* bits is equivalent to division by ``pow(2, n)``.
overflow check.
.. _typeiter: .. _typeiter:
......
...@@ -175,23 +175,24 @@ Ellipsis ...@@ -175,23 +175,24 @@ Ellipsis
object: plain integer object: plain integer
single: OverflowError (built-in exception) single: OverflowError (built-in exception)
These represent numbers in the range -2147483648 through 2147483647. (The range These represent numbers in the range -2147483648 through 2147483647.
may be larger on machines with a larger natural word size, but not smaller.) (The range may be larger on machines with a larger natural word size,
When the result of an operation would fall outside this range, the result is but not smaller.) When the result of an operation would fall outside
normally returned as a long integer (in some cases, the exception this range, the result is normally returned as a long integer (in some
:exc:`OverflowError` is raised instead). For the purpose of shift and mask cases, the exception :exc:`OverflowError` is raised instead). For the
operations, integers are assumed to have a binary, 2's complement notation using purpose of shift and mask operations, integers are assumed to have a
32 or more bits, and hiding no bits from the user (i.e., all 4294967296 binary, 2's complement notation using 32 or more bits, and hiding no
different bit patterns correspond to different values). bits from the user (i.e., all 4294967296 different bit patterns
correspond to different values).
Long integers Long integers
.. index:: object: long integer .. index:: object: long integer
These represent numbers in an unlimited range, subject to available (virtual) These represent numbers in an unlimited range, subject to available
memory only. For the purpose of shift and mask operations, a binary (virtual) memory only. For the purpose of shift and mask operations, a
representation is assumed, and negative numbers are represented in a variant of binary representation is assumed, and negative numbers are represented
2's complement which gives the illusion of an infinite string of sign bits in a variant of 2's complement which gives the illusion of an infinite
extending to the left. string of sign bits extending to the left.
Booleans Booleans
.. index:: .. index::
...@@ -199,20 +200,22 @@ Ellipsis ...@@ -199,20 +200,22 @@ Ellipsis
single: False single: False
single: True single: True
These represent the truth values False and True. The two objects representing These represent the truth values False and True. The two objects
the values False and True are the only Boolean objects. The Boolean type is a representing the values False and True are the only Boolean objects.
subtype of plain integers, and Boolean values behave like the values 0 and 1, The Boolean type is a subtype of plain integers, and Boolean values
respectively, in almost all contexts, the exception being that when converted to behave like the values 0 and 1, respectively, in almost all contexts,
a string, the strings ``"False"`` or ``"True"`` are returned, respectively. the exception being that when converted to a string, the strings
``"False"`` or ``"True"`` are returned, respectively.
.. index:: pair: integer; representation .. index:: pair: integer; representation
The rules for integer representation are intended to give the most meaningful The rules for integer representation are intended to give the most
interpretation of shift and mask operations involving negative integers and the meaningful interpretation of shift and mask operations involving negative
least surprises when switching between the plain and long integer domains. Any integers and the least surprises when switching between the plain and long
operation except left shift, if it yields a result in the plain integer domain integer domains. Any operation, if it yields a result in the plain
without causing overflow, will yield the same result in the long integer domain integer domain, will yield the same result in the long integer domain or
or when using mixed operands. when using mixed operands. The switch between domains is transparent to
the programmer.
:class:`numbers.Real` (:class:`float`) :class:`numbers.Real` (:class:`float`)
.. index:: .. index::
......
...@@ -944,11 +944,9 @@ by the number of bits given by the second argument. ...@@ -944,11 +944,9 @@ by the number of bits given by the second argument.
.. index:: exception: ValueError .. index:: exception: ValueError
A right shift by *n* bits is defined as division by ``pow(2,n)``. A left shift A right shift by *n* bits is defined as division by ``pow(2, n)``. A left shift
by *n* bits is defined as multiplication with ``pow(2,n)``; for plain integers by *n* bits is defined as multiplication with ``pow(2, n)``. Negative shift
there is no overflow check so in that case the operation drops bits and flips counts raise a :exc:`ValueError` exception.
the sign if the result is not less than ``pow(2,31)`` in absolute value.
Negative shift counts raise a :exc:`ValueError` exception.
.. _bitwise: .. _bitwise:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment