New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unexpected output from arange with dtype=int #16159
Comments
Bugs like this are reported over and over again. For reasons lost to time, I'm fairly confident the implementation of arange is something like: def arange(start, stop, step, dtype):
n = (start - stop) // step
# dtype.type is a cast
step = dtype.type(start + step) - dtype.type(start)
# now do what you expect
return [start + step*i for i in range(n)] |
Perhaps we should add that pseudo-code to the documentation? |
Yeah, that code is correct (not 100% sure about the
In the end, I guess I would like a well thought out proposal :/... |
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: 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
|
@aryanxk02 We reviewed the solution you proposed at today's triage meeting. It wouldn't solve the issue. Thank you for giving it a go! |
In [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:
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?
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]
The text was updated successfully, but these errors were encountered: