Skip to content

Commit c914062

Browse files
authored
handle the condition in in when step(r::AbstractRange) equals 0 (#41974)
* handle the condition in `in` when `step` equals 0 Using `in` when `step(r::AbstractRange) == 0` results in an error. Handle this case by using boolean logic for both the definitions to which this case applies (`x::Real` and `x::AbstractChar`). First, check if the step equals zero and if `x` (the value) equals the reference value before performing the invalid `mod` operation. Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com> * add tests for `StepRangeLen` when step equals 0 Add two tests (one which passes and another which fails) each, for `in` with `StepRangeLen` that has step = 0, where `x` is of type `Real` and `AbstractChar`. Signed-off-by: Anant Thazhemadam <anant.thazhemadam@gmail.com>
1 parent 11fabf1 commit c914062

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

base/range.jl

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1382,11 +1382,13 @@ in(x::T, r::AbstractRange{T}) where {T} = _in_range(x, r)
13821382
in(x::Integer, r::AbstractUnitRange{<:Integer}) = (first(r) <= x) & (x <= last(r))
13831383

13841384
in(x::Real, r::AbstractRange{T}) where {T<:Integer} =
1385-
isinteger(x) && !isempty(r) && x >= minimum(r) && x <= maximum(r) &&
1386-
(mod(convert(T,x),step(r))-mod(first(r),step(r)) == 0)
1385+
isinteger(x) && !isempty(r) &&
1386+
(iszero(step(r)) ? x == first(r) : (x >= minimum(r) && x <= maximum(r) &&
1387+
(mod(convert(T,x),step(r))-mod(first(r),step(r)) == 0)))
13871388
in(x::AbstractChar, r::AbstractRange{<:AbstractChar}) =
1388-
!isempty(r) && x >= minimum(r) && x <= maximum(r) &&
1389-
(mod(Int(x) - Int(first(r)), step(r)) == 0)
1389+
!isempty(r) &&
1390+
(iszero(step(r)) ? x == first(r) : (x >= minimum(r) && x <= maximum(r) &&
1391+
(mod(Int(x) - Int(first(r)), step(r)) == 0)))
13901392

13911393
# Addition/subtraction of ranges
13921394

test/ranges.jl

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,3 +2164,20 @@ end
21642164
@test length(range(1, 100, length=big(100)^100)) == big(100)^100
21652165
@test length(range(big(1), big(100)^100, length=big(100)^100)) == big(100)^100
21662166
@test length(0 * (1:big(100)^100)) == big(100)^100
2167+
2168+
@testset "issue #41784" begin
2169+
# tests `in` when step equals 0
2170+
# test for Int
2171+
x = 41784
2172+
@test (x in StepRangeLen(x, 0, 0)) == false
2173+
@test (x in StepRangeLen(x, 0, rand(1:100))) == true
2174+
@test ((x - 1) in StepRangeLen(x, 0, rand(1:100))) == false
2175+
@test ((x + 1) in StepRangeLen(x, 0, rand(1:100))) == false
2176+
2177+
# test for Char
2178+
x = 'z'
2179+
@test (x in StepRangeLen(x, 0, 0)) == false
2180+
@test (x in StepRangeLen(x, 0, rand(1:100))) == true
2181+
@test ((x - 1) in StepRangeLen(x, 0, rand(1:100))) == false
2182+
@test ((x + 1) in StepRangeLen(x, 0, rand(1:100))) == false
2183+
end

0 commit comments

Comments
 (0)