Skip to content

Commit

Permalink
Merge pull request #11261 from JuliaLang/mn/10950
Browse files Browse the repository at this point in the history
fix #10950 (UnitRange(1//2,3) bug)
  • Loading branch information
nolta committed May 14, 2015
2 parents 2be6cc4 + f0a8b59 commit ff35d98
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
13 changes: 8 additions & 5 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -66,14 +66,17 @@ StepRange{T,S}(start::T, step::S, stop::T) = StepRange{T,S}(start, step, stop)
immutable UnitRange{T<:Real} <: OrdinalRange{T,Int}
start::T
stop::T

UnitRange(start, stop) =
new(start, ifelse(stop >= start, stop, convert(T,start-one(stop-start))))

UnitRange(start::Bool, stop::Bool) = new(start, stop)
UnitRange(start, stop) = new(start, unitrange_last(start,stop))
end
UnitRange{T<:Real}(start::T, stop::T) = UnitRange{T}(start, stop)

unitrange_last(::Bool, stop::Bool) = stop
unitrange_last{T<:Integer}(start::T, stop::T) =
ifelse(stop >= start, stop, convert(T,start-one(stop-start)))
unitrange_last{T}(start::T, stop::T) =
ifelse(stop >= start, convert(T,start+floor(stop-start)),
convert(T,start-one(stop-start)))

colon(a::Real, b::Real) = colon(promote(a,b)...)

colon{T<:Real}(start::T, stop::T) = UnitRange{T}(start, stop)
Expand Down
10 changes: 10 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -531,3 +531,13 @@ let io = IOBuffer()
str = takebuf_string(io)
@test str == "linspace(1.0,2.0,3)"
end

# issue 10950
r = 1//2:3
@test length(r) == 3
i = 1
for x in r
@test x == i//2
i += 2
end
@test i == 7

0 comments on commit ff35d98

Please sign in to comment.