-
-
Notifications
You must be signed in to change notification settings - Fork 5.6k
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
ranges with ambiguous lengths #20532
Comments
This test was occasionally failing (~1 in 10 million times) since there are cases where the length of such a range is ambiguous, i.e. there is more than one `n` such that `start + (n-1)*step == stop`. This adjusts the test to allow any such choice of range length.
This test was occasionally failing (~1 in 10 million times) since there are cases where the length of such a range is ambiguous, i.e. there is more than one `n` such that `start + (n-1)*step == stop`. This adjusts the test to allow any such choice of range length.
This test was occasionally failing (~1 in 10 million times) since there are cases where the length of such a range is ambiguous, i.e. there is more than one `n` such that `start + (n-1)*step == stop`. This adjusts the test to allow any such choice of range length.
I think that the closest viable value of |
Fixes #20532 by defining which length to use when there's more than one length that is compatible with a given start:step:stop combo. This also tightens the tests to check for that specific result.
Alternative suggestion, should an |
just by curiosity: Does anyone know how matlab adresses this? I run the same code over a wide range of systems and OSs and the only thing i (afair) never had to revisit was linspace or ranges. |
Always returns a length-1 range: >> a = single(1.3173739)
a =
1.3174
>> st = single(-2.3759904e-8)
st =
-2.3760e-8
>> a:st:a
ans =
1.3174
>> linspace(a, a, 5)
ans =
1.3174 1.3174 1.3174 1.3174 1.3174 |
@lobingera: they do a hacky thing where if they hit the stop within a few arbitrary number of ulps, they consider it exact. Note that our implementation already works well enough that we essentially never get bug reports about it. This came up because I added more thorough tests that are wildly pathological some very rare fraction of the time. I have to run these tests tens of millions of times before I encounter these cases, so I'm not surprised you haven't encountered them in practice. |
@oscardssmith – it's a reasonable suggestion, but I think there are two cases here:
So picking a sensible choice like the length closest to |
moving the backport label to the PR #20533 |
(cherry picked from commit b7ad743)
This is the 0.5 edition: with this change a, b, n are always hit exactly by a:s:a+n*s construction tests. There are two parts to the fix: 1) check that the lifted rational endpoint is exact even *after* we've reduced terms of the fractions; 2) apply the same fix that I've proposed for the length in the non-lifted case. Combined backport of these commits: - e849169 - b7ad743 range tests: allow any range length that hits stop (#20532) (cherry picked from commit b7ad743)
Uncommon failure case for range tests, observed by @JeffBezanson turns out to be somewhat philosophical: there are cases where
n
such thatstart + (n-1)*step == stop
is not unique even whenstep != 0
. Because floating-point. So the philosophical part is what should the length of a range be in such a case? The smallest viable choice ofn
? The largest viable choice ofn
?The text was updated successfully, but these errors were encountered: