8000 NEP: Address more of Stéfan's review comments · rjeb/numpy@63f006a · GitHub
[go: up one dir, main page]

Skip to content

Commit 63f006a

Browse files
committed
NEP: Address more of Stéfan's review comments
1 parent 41a2134 commit 63f006a

File tree

1 file changed

+26
-9
lines changed

1 file changed

+26
-9
lines changed

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

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,22 @@ Promotion always occurs along the green lines:
8181
from left to right within their kind and to a higher kind only when
8282
necessary.
8383
The result kind is always the largest kind of the inputs.
84-
Note that NumPy considers ``float32`` to have a lower precision than ``int32``
85-
and ``uint32``, but makes an exception for ``float64`` (and higher).
84+
Note that ``float32`` has a lower precision than ``int32`` or ``uint32`` and
85+
is thus sorted slightly to the left in the schematic. This is because
86+
``float32`` cannot represent all ``int32`` values exactly.
87+
However, for practical reasons, NumPy allows promoting ``int64`` to ``float64``
88+
effectively considering them to have the same precision.
8689

8790
The Python scalars are inserted at the very left of each "kind" and the
88-
Python integer does not distinguish signed and unsigned.
91+
Python integer does not distinguish signed and unsigned. NumPy promotion
92+
thus uses the following, ordered, kind categories:
93+
94+
* `boolean`
95+
* `integral`: signed or unsigned integers
96+
* `inexact`: floating point numbers and complex floating point numbers
97+
8998
When promoting a Python scalar with a dtype of lower kind
90-
category (boolean, integral, inexact) with a higher one, we use the
99+
category (`boolean < integral < inexact`) with a higher one, we use the
91100
minimum/default precision: that is ``float64``, ``complex128`` or ``int64``
92101
(``int32`` is used on some systems, e.g. windows).
93102

@@ -207,7 +216,7 @@ Motivation and Scope
207216
====================
208217

209218
The motivation for changing the behaviour with respect to inspecting the value
210-
of Python scalars and NumPy scalars/0-D arrays is two-fold:
219+
of Python scalars and NumPy scalars/0-D arrays is three-fold:
211220

212221
1. The special handling of NumPy scalars/0-D arrays as well as the value
213222
inspection can be very surprising to users,
@@ -217,6 +226,8 @@ of Python scalars and NumPy scalars/0-D arrays is two-fold:
217226
Currently, this leads to a dual implementation of a new and an old (value
218227
sensitive) system. Fixing this will greatly simplify the internal logic
219228
and make results more consistent.
229+
3. It largely aligns with the choice of other projects like `JAX` and
230+
`data-apis.org` (see also `Related Work`).
220231

221232
We believe that the proposal of "weak" Python scalars will help users by
222233
providing a clear mental model for which datatype an operation will
@@ -285,8 +296,8 @@ Impact on operators and functions involving NumPy arrays or scalars
285296
-------------------------------------------------------------------
286297

287298
The main impact on operations not involving Python scalars (``float``, ``int``,
288-
``complex``) will be that 0-D arrays and NumPy scalars will never behave
289-
value-sensitive.
299+
``complex``) will be that operations on 0-D arrays and NumPy scalars will never
300+
depend on their values.
290301
This removes currently surprising cases. For example::
291302

292303
np.arange(10, dtype=np.uint8) + np.int64(1)
@@ -545,15 +556,19 @@ At no time is the value used to decide the result of this promotion. The value
545556
considered when it is converted to the new dtype; this may raise an error.
546557

547558

548-
549-
550559
Related Work
551560
============
552561

562+
Different Python projects that fill a similar space to NumPy prefer the weakly
563+
typed Python scalars as proposed in this NEP. Details of these may differ
564+
or be unspecified though:
565+
553566
* `JAX promotion`_ also uses the weak-scalar concept. However, it makes use
554567
of it also for most functions. JAX further stores the "weak-type" information
555568
on the array: ``jnp.array(1)`` remains weakly typed.
556569

570+
* `data-apis.org`_ also suggests this weak-scalar logic for the Python scalars.
571+
557572

558573
Implementation
559574
==============
@@ -736,6 +751,8 @@ References and Footnotes
736751

737752
.. _JAX promotion: https://jax.readthedocs.io/en/latest/type_promotion.html
738753

754+
.. _data-apis.org: https://data-apis.org/array-api/latest/API_specification/type_promotion.html
755+
739756
.. [2] https://github.com/numpy/numpy/pull/21103/files#r814188019
740757
741758
Copyright

0 commit comments

Comments
 (0)
0