Skip to content

Commit

Permalink
Correct _in_range for NaN/Inf (#41169)
Browse files Browse the repository at this point in the history
(cherry picked from commit 9a9e5a3)
  • Loading branch information
sostock authored and KristofferC committed Jun 29, 2021
1 parent 7c1f9b2 commit 9f6aabb
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
4 changes: 3 additions & 1 deletion base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1073,7 +1073,9 @@ function sum(r::AbstractRange{<:Real})
end

function _in_range(x, r::AbstractRange)
if step(r) == 0
if !isfinite(x)
return false
elseif iszero(step(r))
return !isempty(r) && first(r) == x
else
n = round(Integer, (x - first(r)) / step(r)) + 1
Expand Down
5 changes: 5 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,11 @@ end

@test !(1 in 1:0)
@test !(1.0 in 1.0:0.0)

for r = (1:10, 1//1:10//1, 1:2:5, 1//2:1//2:5//2, 1.0:5.0, LinRange(1.5, 5.5, 9)),
x = (NaN16, Inf32, -Inf64, 1//0, -1//0)
@test !(x in r)
end
end
@testset "in() works across types, including non-numeric types (#21728)" begin
@test 1//1 in 1:3
Expand Down

0 comments on commit 9f6aabb

Please sign in to comment.