Skip to content

Commit

Permalink
fix for ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
jishnub committed May 5, 2020
1 parent c655e5d commit 1584c77
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
@@ -1,7 +1,7 @@
name = "MultiplesOfPi"
uuid = "b749d01f-fee9-4313-9f11-89ddf7ea9d58"
authors = ["Jishnu Bhattacharya", "Center for Space Science", "New York University Abu Dhabi"]
version = "0.4.0"
version = "0.4.1"

[compat]
julia = "1"
Expand Down
24 changes: 24 additions & 0 deletions src/MultiplesOfPi.jl
Expand Up @@ -514,6 +514,30 @@ function Base.convert(::Type{PiExpTimes{N,T}},p::PiExpTimes{N,R}) where {N,T<:Re
PiExpTimes{N,T}(p_new)
end

# Fix for LinRange
# Use convert instead of constructor
function Base.lerpi(j::Integer, d::Integer, a::T, b::T) where T<:PiExpTimes
Base.@_inline_meta
t = j/d
convert(T,(1-t)*a + t*b)
end

function Base._range(start::PiExpTimes{M,T},::Nothing,
stop::PiExpTimes{N,T},length::Integer) where {M,N,T}

F = PiExpTimes{min(M,N),float(T)}
start_f = convert(F,start)
stop_f = convert(F,stop)
LinRange{F}(start_f,stop_f,length)
end

function Base.:(:)(start::PiExpTimes{N,T},
step::PiExpTimes{N,T},stop::PiExpTimes{N,T}) where {N,T}

F = PiExpTimes{N,float(T)}
StepRangeLen{F,T,T}(start.x,step.x,floor(Int, (stop.x-start.x)/step.x)+1)
end

# Pretty-printing

function Base.show(io::IO,p::PiExpTimes{N}) where {N}
Expand Down
36 changes: 36 additions & 0 deletions test/runtests.jl
Expand Up @@ -797,6 +797,42 @@ end
end
end

@testset "Range" begin
@testset "LinRange" begin
l = LinRange(0.0Pi,1.0Pi,10)
lpi = LinRange(0,π,10)
@test l[1] === 0.0Pi
@test l[end] === 1.0Pi

for i in eachindex(l,lpi)
@test l[i] lpi[i]
end
end
@testset "range" begin
@testset "length" begin
r = range(0Pi,stop=2Pi,length=2)
@test typeof(r) == LinRange{PiExpTimes{1,Float64}}
@test r[1] === 0.0Pi
@test r[2] === 2.0Pi

r = range(0Pi,stop=2Pi^2,length=2)
@test typeof(r) == LinRange{PiExpTimes{1,Float64}}
@test r[1] === 0.0Pi
@test r[2] === 2π*Pi
end
@testset "step" begin
r = range(0Pi,stop=2Pi,step=Pi)
@test length(r) == 3
@test r[1] === 0.0Pi
@test r[end] === 2.0Pi
@test step(r) === 1.0Pi
end
end
@testset "consistency" begin
@test range(2Pi,stop=20Pi,step=9Pi) == range(2Pi,stop=20Pi,length=3)
end
end

@testset "arithmetic" begin
p = PiTimes(3)
q = PiTimes(4)
Expand Down

0 comments on commit 1584c77

Please sign in to comment.