Skip to content

Commit

Permalink
Merge pull request PainterQubits#208 from rafaqz/missing
Browse files Browse the repository at this point in the history
handle missings
  • Loading branch information
ajkeller34 authored Feb 25, 2019
2 parents 3e6f37d + 58450d7 commit c01affa
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/conversion.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ function uconvert(a::Units, x::Number)
end
end

uconvert(a::Units, x::Missing) = missing

@generated function uconvert_affine(a::Units, x::Quantity)
# TODO: test, may be able to get bad things to happen here when T<:LogScaled
auobj = a()
Expand Down
10 changes: 10 additions & 0 deletions src/units.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,14 @@ true
*(a0::Units, a::Units...) = FixedUnits(*(FreeUnits(a0), FreeUnits.(a)...))
# Logic above is that if we're not using FreeOrContextUnits, at least one is FixedUnits.

*(a0::Units, a::Missing) = missing
*(a0::Missing, a::Units) = missing

/(x::Units, y::Units) = *(x,inv(y))

/(x::Units, y::Missing) = missing
/(x::Missing, y::Units) = missing

//(x::Units, y::Units) = x/y

# Both methods needed for ambiguity resolution
Expand All @@ -116,6 +123,9 @@ true
^(x::FixedUnits{N,D,nothing}, y::Integer) where {N,D} = *(FixedUnits{map(a->a^y, N), ()}())
^(x::FixedUnits{N,D,nothing}, y::Number) where {N,D} = *(FixedUnits{map(a->a^y, N), ()}())

^(x::Units, y::Missing) = missing
^(x::Missing, y::Units) = missing

Base.literal_pow(::typeof(^), x::AffineUnits, ::Val{p}) where p =
throw(AffineError("an invalid operation was attempted with affine units: $x"))

Expand Down
6 changes: 6 additions & 0 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ true
"""
@inline ustrip(x::Number) = x / unit(x)
@inline ustrip(x::Quantity) = ustrip(x.val)
@inline ustrip(x::Missing) = missing

"""
ustrip(x::Array{Q}) where {Q <: Quantity}
Expand Down Expand Up @@ -130,6 +131,9 @@ true
"""
@inline unit(x::Number) = NoUnits
@inline unit(x::Type{T}) where {T <: Number} = NoUnits
@inline unit(x::Type{Union{Missing, T}}) where T = unit(T)
@inline unit(x::Type{Missing}) = missing
@inline unit(x::Missing) = missing

"""
absoluteunit(::Units)
Expand Down Expand Up @@ -166,6 +170,8 @@ true
"""
@inline dimension(x::Number) = NoDims
@inline dimension(x::Type{T}) where {T <: Number} = NoDims
@inline dimension(x::Missing) = missing
@inline dimension(x::Type{Missing}) = missing

"""
dimension(u::Units{U,D}) where {U,D}
Expand Down
9 changes: 9 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,10 @@ end
@test @inferred(unit(1m^2)) === m^2
@test @inferred(unit(typeof(1m^2))) === m^2
@test @inferred(unit(Float64)) === NoUnits
@test @inferred(unit(Union{typeof(1m^2),Missing})) === m^2
@test @inferred(unit(Union{Float64,Missing})) === NoUnits
@test @inferred(unit(missing)) === missing
@test @inferred(unit(Missing)) === missing
@test @inferred(dimension(1m^2)) === 𝐋^2
@test @inferred(dimension(1*ContextUnits(m,km)^2)) === 𝐋^2
@test @inferred(dimension(typeof(1m^2))) === 𝐋^2
Expand All @@ -389,6 +393,8 @@ end
@test @inferred(dimension(m/s)) === 𝐋/𝐓
@test @inferred(dimension(1u"mol")) === 𝐍
@test @inferred(dimension(ΞΌm/m)) === NoDims
@test @inferred(dimension(missing)) === missing
@test @inferred(dimension(Missing)) === missing
@test dimension.([1u"m", 1u"s"]) == [𝐋, 𝐓]
@test dimension.([u"m", u"s"]) == [𝐋, 𝐓]
@test (𝐋/𝐓)^2 === 𝐋^2 / 𝐓^2
Expand Down Expand Up @@ -508,6 +514,8 @@ end
@test (m//2) === 1//2 * m # Unit // Real
@test (2//m) === (2//1) / m # Real // Unit
@test (m//s) === m/s # Unit // Unit
@test m / missing === missing # Unit / missing
@test missing / m === missing # Missing / Unit (// is not defined for Missing)
@test @inferred(div(10m, -3cm)) === -333
@test @inferred(fld(10m, -3cm)) === -334
@test rem(10m, -3cm) == 1.0cm
Expand Down Expand Up @@ -1409,6 +1417,7 @@ end
@test ustrip(20dB) === 20
@test ustrip(20dB_rp) === 20
@test ustrip(13dBm) β‰ˆ 13
@test ustrip(missing) === missing
end

@testset "> Display" begin
Expand Down

0 comments on commit c01affa

Please sign in to comment.