question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Unexpected output from arange with dtype=int

See original GitHub issue

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:

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:open
  • Created 3 years ago
  • Comments:5 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
davrotcommented, May 5, 2020

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.

0reactions
aryanxk02commented, Nov 5, 2020

Hey I’m a complete beginner to open source contribution. Thought of giving it a try. How about this snippet? @eric-wieser

x = []
for i in range(start, stop):
    x.append(i)
    x.append(i+step)
print(np.array(x, dtype))

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)]
Read more comments on GitHub >

github_iconTop Results From Across the Web

numpy arange step iteration error logging for dtype=int vs float
Answer to my question is as what hpaulj mentioned, in correct use of arange could lead to unexpected behavior. decimal point for int...
Read more >
numpy.arange — NumPy v1.24 Manual
This can lead to unexpected behaviour. For example: >>> np.arange(0, 5, 0.5, dtype=int) array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0])...
Read more >
NumPy: Create an ndarray with all elements initialized with ...
NumPy: Arrange ndarray in tiles with np.tile() ... array does not match the type of the value to be assigned, unexpected results may...
Read more >
NumPy arange(): How to Use np.arange() - Real Python
In this step-by-step tutorial, you'll learn how to use the NumPy arange() function, ... Parameters and Outputs; Creating Sequences; Python for Loops.
Read more >
numpysane · PyPI
a = np.arange(6).reshape(2,3) ... this creates unexpected results. ... out = inner_product( np.arange(3), np.arange(2*4*3).reshape(2,4,3), dtype=int)
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found