numpy.arange stop precision issue
See original GitHub issueAll elements of the array generated by numpy.arange(start, stop, step)
should be strictly smaller than stop
.
On my Ubuntu 14.04 installation this happened:
In [117]: np.arange(0.5, 0.8, 0.1)
Out[117]: array([ 0.5, 0.6, 0.7, 0.8])
In [118]: np.arange(0.5, 0.9, 0.1)
Out[118]: array([ 0.5, 0.6, 0.7, 0.8])
In [123]: np.arange(0.5, 0.8, 0.1)[-1]
Out[123]: 0.79999999999999993
In [124]: np.arange(0.5, 0.9, 0.1)[-1]
Out[124]: 0.79999999999999993
Is this an issue on other installations?
Issue Analytics
- State:
- Created 8 years ago
- Comments:34 (15 by maintainers)
Top Results From Across the Web
How to prevent float imprecision from affecting numpy.arange?
to determine the number of items, a small float imprecision (stop = . 400000001) can add an unintended value to the list.
Read more >numpy.arange — NumPy v1.24 Manual
Return evenly spaced values within a given interval. arange can be called with a varying number of positional arguments: arange(stop) : Values are...
Read more >NumPy arange(): How to Use np.arange() - Real Python
The arguments of NumPy arange() that define the values contained in the array correspond to the numeric parameters start , stop , and...
Read more >Numpy.arange() decimal precision : r/learnpython - Reddit
import numpy as np X=np.arange(0,1.1,0.1) print(X) >>[0. ... Is the best way to handle the error just by rounding it off (at least...
Read more >torch.arange — PyTorch 1.13 documentation
torch.arange. torch.arange(start=0, end, step=1, *, out=None, dtype=None, layout=torch.strided, device=None, requires_grad=False) → Tensor.
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@jaimefrio: I would definitely vote to reopen this issue. It is impossible that the function depends on the implementation and some rounding luck! Or let’s put it this way: Numpy is supposed to be a “mathematical-operations”-library. If a function then is supposed to return a list from 0.5 to 0.8 excluding 0.8 and with stepsize 0.1, it has to return a list with exactly three components, [0.5, 0.6, 0.7] (yes, actually [0.50000001, 0.6, 0.69999993] or similar, but definitely not an additional number]). I agree, that rounding errors can make life more difficult when doing calculations, but ignoring them actively and basically saying “deal with the problem yourself” is definitely not the solution. Instead, arange has to recognize rounding errors, otherwise, the function is not usable (search the web, a lot of people complain and work around this error).
A possible implementation could have an additional argument, something like “stopping_precision”, that defines the “stopping-range”.
I love numpy and I would really like to see arange to be usable again. Or, honestly, how many functions do you regularly use whose return depends on… luck? And on the other hand, if you call it a “feature”: please name one single use-case for this “feature” 😉
I think you mean hardly ever exact.