Skip to content

Commit

Permalink
Define upreferred(::TwicePrecision) (#577)
Browse files Browse the repository at this point in the history
  • Loading branch information
sostock authored Nov 28, 2022
1 parent 0e783dc commit 49e6dde
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
3 changes: 3 additions & 0 deletions src/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ _colon(::Any, ::Any, start::T, step, stop::T) where {T} =
*(x::Base.TwicePrecision, y::Units) = Base.TwicePrecision(x.hi*y, x.lo*y)
*(x::Base.TwicePrecision, y::Quantity) = (x * ustrip(y)) * unit(y)
uconvert(y, x::Base.TwicePrecision) = Base.TwicePrecision(uconvert(y, x.hi), uconvert(y, x.lo))
@inline upreferred(x::Base.TwicePrecision{T}) where T<:Number = x
@inline upreferred(x::Base.TwicePrecision{T}) where T<:AbstractQuantity =
uconvert(upreferred(unit(x)), x)

colon(start::T, step::T, stop::T) where {T<:Quantity{<:Base.IEEEFloat}} =
colon(ustrip(start), ustrip(step), ustrip(stop)) * unit(T) # This will always return a StepRangeLen
Expand Down
5 changes: 5 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1214,6 +1214,11 @@ end
@test @inferred((1:2:5) .* cm .|> mm) === 10mm:20mm:50mm
@test mm.((1:2:5) .* cm) === 10mm:20mm:50mm
@test @inferred((1:2:5) .* km .|> upreferred) === 1000m:2000m:5000m
@test @inferred((1:2:5)km .|> upreferred) === 1000m:2000m:5000m
@test @inferred((1:2:5) .|> upreferred) === 1:2:5
@test @inferred((1.0:2.0:5.0) .* km .|> upreferred) === 1000.0m:2000.0m:5000.0m
@test @inferred((1.0:2.0:5.0)km .|> upreferred) === 1000.0m:2000.0m:5000.0m
@test @inferred((1.0:2.0:5.0) .|> upreferred) === 1.0:2.0:5.0
@test @inferred((1:2:5) .* cm .|> mm .|> ustrip) === 10:20:50
@test @inferred((1f0:2f0:5f0) .* cm .|> mm .|> ustrip) === 10f0:20f0:50f0
end
Expand Down

0 comments on commit 49e6dde

Please sign in to comment.