8000 DOC: Fix docs that cause an integer overflow · numpy/numpy@afd0b30 · GitHub
[go: up one dir, main page]

Skip to content

Commit afd0b30

Browse files
committed
DOC: Fix docs that cause an integer overflow
1 parent 28f1222 commit afd0b30

File tree

1 file changed

+10
-5
lines changed

1 file changed

+10
-5
lines changed

doc/source/user/basics.creation.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,13 +43,18 @@ When you use :func:`numpy.array` to define a new array, you should
4343
consider the :doc:`dtype <basics.types>` of the elements in the array,
4444
which can be specified explicitly. This feature gives you
4545
more control over the underlying data structures and how the elements
46-
are handled in C/C++ functions. If you are not careful with ``dtype``
47-
assignments, you can get unwanted overflow, as such
46+
are handled in C/C++ functions.
47+
When values do not fit and you are using a ``dtype`` NumPy may raise an
48+
error::
4849

49-
::
50+
>>> np.array([127, 128, 129], dtype=np.int8)
51+
Traceback (most recent call last):
52+
...
53+
OverflowError: Python integer 128 out of bounds for int8
54+
55+
However, NumPy integers are force-cast and may silently overflow::
5056

51-
>>> a = np.array([127, 128, 129], dtype=np.int8)
52-
>>> a
57+
>>> np.array([127, np.int64(128), np.int64(129)], dtype=np.int8)
5358
array([ 127, -128, -127], dtype=int8)
5459

5560
An 8-bit signed integer represents integers from -128 to 127.

0 commit comments

Comments
 (0)
0