Unexpected output from arange with dtype=int
See original GitHub issueIn [3]: np.arange(-3, 0, 0.5, dtype=int)
Out[3]: array([-3, -2, -1, 0, 1, 2])
Well, to see a “1” and a “2” was a bit unexpected for us since both numbers are a bit bigger than 0.
Normally, this is the result without dtype=int:
In [2]: np.arange(-3, 0, 0.5)
Out[2]: array([-3. , -2.5, -2. , -1.5, -1. , -0.5])
and we should get this with dtype=int:
In [4]: np.arange(-3, 0, 0.5).astype(int)
Out[4]: array([-3, -2, -2, -1, -1, 0])
The numpy manual states: dtype : dtype The type of the output array. If dtype is not given, infer the data type from the other input arguments.
Thus it should only effect the output array, right?
import numpy as np
print(np.arange(-3, 0, 0.5))
print(np.arange(-3, 0, 0.5, dtype=int))
print(np.arange(-3, 0, 0.5).astype(int))
Error message:
No error message…
Numpy/Python version information:
We tested it under numpy ‘1.18.4’ (pure Python 3.7.6) as well as ‘1.18.1’ (Anaconda 3.7 with the latest update applied). Same result.
1.18.4 3.7.6 (default, Feb 28 2020, 15:25:38) [Clang 11.0.0 (https://github.com/llvm/llvm-project.git eefbff0082c5228e01611f7
1.18.1 3.7.4 (default, Aug 13 2019, 20:35:49) [GCC 7.3.0]
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (3 by maintainers)
Getting values that are bigger than “stop” is really not nice and a bit unexpected. If arange is not for float, you could check for the floaty numpy types and raise an exception.
Also the manual entry for dtype really lets the user expect something like an astype(dtype) conversion of only the output.
How about: 1.) Exception for non-integer arguments (i.e. start, stop, step). 2.) Check if stop >= start, otherwise raise an exception 3.) Cast start, stop, step to int64 in the beginning of the function. 4.) astype(dtype) the output
Instead of 1.) you can redirect to linspace inside of arange if a non-integer input is found.
Hey I’m a complete beginner to open source contribution. Thought of giving it a try. How about this snippet? @eric-wieser