8000 NEP: Make a note of Python integer "exceptions" · mattip/numpy@5b2207e · GitHub
[go: up one dir, main page]

Skip to content

Commit 5b2207e

Browse files
committed
NEP: Make a note of Python integer "exceptions"
These are not really exceptions to the rules, but may look like exceptions when compared to the behavior of other operations. Also briefly notes that we should probably aspire to allowing such "exceptions", but I don't think we should be beholden to make it possible in every case (some of these are non-trivial to make work!). Also added one sentence that this applies to everything of course.
1 parent 0a4b2b8 commit 5b2207e

File tree

1 file changed

+24
-0
lines changed

1 file changed

+24
-0
lines changed

doc/neps/nep-0050-scalar-promotion.rst

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ following hold::
163163
True + np.uint8(2) == np.uint8(3)
164164

165165

166+
Note that while this NEP uses simple operators as example, the rules described
167+
generally apply to all of NumPy operations.
168+
166169
Table comparing new and old behaviour
167170
-------------------------------------
168171

@@ -401,6 +404,27 @@ It will then continue to do the calculation with ``inf`` as usual.
401404
set up to raise an error due to the overflow, however).
402405
It is also for example the behaviour of ``pytorch`` 1.10.
403406

407+
Particular behavior of Python integers
408+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
409+
410+
The NEPs promotion rules stated in terms of the resulting dtype which is
411+
typically also the operation dtype (in terms of result precision).
412+
This leads to what may seem like exceptions for Python integers:
413+
While ``uint8(3) + 1000`` must be rejected because operating
414+
in ``uint8`` is not possible, ``uint8(3) / 1000`` returns a ``float64`` and
415+
can convert both inputs to ``float64`` to find the result.
416+
417+
In practice this means that arbitrary Python integer values are accepted in
418+
the following cases:
419+
* All comparisons (``==``, ``<``, etc.) between NumPy and Python integers are
420+
always well defined.
421+
* Unary functions like ``np.sqrt`` that give a floating point result can and
422+
will convert the Python integer to a float.
423+
* Division of integers returns floating point by casting input to ``float64``.
424+
425+
Note that there may be additional functions where these exceptions could be
426+
applied but are not. In these cases it should be considered an improvement
427+
to allow them, but when the user impact is low we may not do so for simplicity.
404428

405429

406430
Backward compatibility

0 commit comments

Comments
 (0)
0