Skip to content

Commit 9a9e5a3

Browse files
authored
Correct _in_range for NaN/Inf (#41169)
1 parent cd68571 commit 9a9e5a3

File tree

2 files changed

+8
-1
lines changed

2 files changed

+8
-1
lines changed

base/range.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1217,7 +1217,9 @@ function sum(r::AbstractRange{<:Real})
12171217
end
12181218

12191219
function _in_range(x, r::AbstractRange)
1220-
if step(r) == 0
1220+
if !isfinite(x)
1221+
return false
1222+
elseif iszero(step(r))
12211223
return !isempty(r) && first(r) == x
12221224
else
12231225
n = round(Integer, (x - first(r)) / step(r)) + 1

test/ranges.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,11 @@ end
459459

460460
@test !(1 in 1:0)
461461
@test !(1.0 in 1.0:0.0)
462+
463+
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)),
464+
x = (NaN16, Inf32, -Inf64, 1//0, -1//0)
465+
@test !(x in r)
466+
end
462467
end
463468
@testset "in() works across types, including non-numeric types (#21728)" begin
464469
@test 1//1 in 1:3

0 commit comments

Comments
 (0)