Skip to content

Commit 19626b0

Browse files
authored
Update type casting section for interaction with Python scalars (#74)
1 parent 56d0058 commit 19626b0

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

spec/API_specification/array_object.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ A conforming implementation of the array API standard must provide and support a
1515

1616
* * *
1717

18+
(operators)=
19+
1820
## Operators
1921

2022
A conforming implementation of the array API standard must provide and support an array object supporting the following Python operators:

spec/API_specification/type_promotion.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,11 @@ In code, use the data type objects specified in {ref}`data-types` (e.g., `int16`
1818

1919
<!-- Note: please keep table columns aligned -->
2020

21+
The following type promotion tables specify the casting behaviour for
22+
operations involving two arrays. In situations where more than two arrays
23+
participate, the table can be used repeatedy on pairs of input arrays (the
24+
result does not depend on the order in which the arrays are given).
25+
2126
### Signed integer type promotion table
2227

2328
| | i1 | i2 | i4 | i8 |
@@ -82,3 +87,27 @@ where
8287
Mixed integer and floating-point type promotion rules are not specified
8388
because behavior varies between implementations.
8489
```
90+
91+
### Mixing arrays with Python scalars
92+
93+
Using Python scalars (i.e. instances of `bool`, `int`, `float`) together with arrays must be supported for:
94+
95+
- `array <op> scalar`,
96+
- `scalar <op> array`,
97+
98+
where `<op>` is a built-in operator (see {ref}`operators` for operators
99+
supported by the array object), and `scalar` is of the same kind as the array
100+
dtype (e.g. a `float` scalar if the array's dtype is `float32` or `float64`).
101+
The expected behaviour is then equivalent to:
102+
103+
1. Convert the scalar to a 0-D array with the same dtype as that of the array
104+
used in the expression.
105+
2. Execute the operation for `array <op> 0-D array` (or `0-D array <op>
106+
array` if `scalar` was the left-hand argument).
107+
108+
```{note}
109+
110+
Note again that mixed integer and floating-point behaviour is not specified.
111+
Mixing an integer array with a Python float may give `float32`, `float64`,
112+
or raise an exception - behaviour of implementations will differ.
113+
```

0 commit comments

Comments
 (0)