Skip to content

Commit 578ca66

Browse files
authored
raise exception in arange, if step size is 0 (#582)
1 parent 33398e0 commit 578ca66

File tree

5 files changed

+20
-2
lines changed

5 files changed

+20
-2
lines changed

code/numpy/create.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,12 @@ mp_obj_t create_arange(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_arg
152152
if(args[3].u_obj != mp_const_none) {
153153
dtype = (uint8_t)mp_obj_get_int(args[3].u_obj);
154154
}
155+
156+
// bail out, if the range cannot be constructed
157+
if(step == MICROPY_FLOAT_CONST(0.0)) {
158+
mp_raise_msg(&mp_type_ZeroDivisionError, MP_ERROR_TEXT("divide by zero"));
159+
}
160+
155161
ndarray_obj_t *ndarray;
156162
if((stop - start)/step < 0) {
157163
ndarray = ndarray_new_linear_array(0, dtype);

code/ulab.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
#include "user/user.h"
3434
#include "utils/utils.h"
3535

36-
#define ULAB_VERSION 6.0.5
36+
#define ULAB_VERSION 6.0.6
3737
#define xstr(s) str(s)
3838
#define str(s) #s
3939

docs/ulab-change-log.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1+
Sun, 21 Jan 2023
2+
3+
version 6.0.6
14

5+
raise proper exception in arange
6+
27
Sun, 21 Jan 2023
38

49
version 6.0.5

tests/2d/numpy/arange.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,10 @@
88
for dtype in dtypes:
99
print(np.arange(10, dtype=dtype))
1010
print(np.arange(2, 10, dtype=dtype))
11-
print(np.arange(2, 10, 3, dtype=dtype))
11+
print(np.arange(2, 10, 3, dtype=dtype))
12+
13+
# test for ZeroDivisionError exception
14+
try:
15+
np.arange(0, 10, 0)
16+
except ZeroDivisionError as e:
17+
print('ZeroDivisionError: ', e)

tests/2d/numpy/arange.py.exp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,4 @@ array([2, 5, 8], dtype=int16)
1313
array([0.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
1414
array([2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0], dtype=float64)
1515
array([2.0, 5.0, 8.0], dtype=float64)
16+
ZeroDivisionError: divide by zero

0 commit comments

Comments
 (0)