Skip to content

Commit

Permalink
Quick return for linspace when start == stop. Fixes JuliaLang#20520. (J…
Browse files Browse the repository at this point in the history
…uliaLang#20537)

The old code worked in the case where the inputs are expressible as rationals, but when that fails we were getting an InexactError.
  • Loading branch information
timholy authored and tkelman committed Feb 9, 2017
1 parent 15a1d7f commit 3bc9a16
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
3 changes: 3 additions & 0 deletions base/twiceprecision.jl
Original file line number Diff line number Diff line change
Expand Up @@ -318,6 +318,9 @@ end
# For Float16, Float32, and Float64, linspace returns a StepRangeLen
function linspace{T<:Union{Float16,Float32,Float64}}(start::T, stop::T, len::Integer)
len < 2 && return _linspace1(T, start, stop, len)
if start == stop
return StepRangeLen(TwicePrecision(start,zero(T)), TwicePrecision(zero(T),zero(T)), len)
end
# Attempt to find exact rational approximations
start_n, start_d = rat(start)
stop_n, stop_d = rat(stop)
Expand Down
8 changes: 8 additions & 0 deletions test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -867,6 +867,14 @@ let r = linspace(-big(1.0),big(1.0),4)
@test r[2] big(-1.0)/3
end

# issue #20520
let r = linspace(1.3173739f0, 1.3173739f0, 3)
@test length(r) == 3
@test first(r) === 1.3173739f0
@test last(r) === 1.3173739f0
@test r[2] === 1.3173739f0
end

let r = linspace(1.0, 3+im, 4)
@test r[1] === 1.0+0.0im
@test r[2] (5/3)+(1/3)im
Expand Down

0 comments on commit 3bc9a16

Please sign in to comment.