diff --git a/base/range.jl b/base/range.jl index df484c30dd4c5..924c1ac31ec87 100644 --- a/base/range.jl +++ b/base/range.jl @@ -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) diff --git a/test/ranges.jl b/test/ranges.jl index ef931c854b932..e4dcb20ddbd40 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -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