From 6f9910093f8e484f4aa50ba6396c02944595aa50 Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Mon, 2 May 2022 17:18:37 +0200 Subject: [PATCH 1/3] Add explicit ustrip for logarithmic quantities --- src/logarithm.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/logarithm.jl b/src/logarithm.jl index 58d76beb..508958bb 100644 --- a/src/logarithm.jl +++ b/src/logarithm.jl @@ -160,7 +160,9 @@ function uconvert(a::MixedUnits{Gain{L1,S1,<:Real}}, x::Level{L2,S2}) where {L1, end ustrip(x::Level{L,S}) where {L<:LogInfo,S} = tolog(L,S,x.val/reflevel(x)) +ustrip(u::MixedUnits, x::Level) = ustrip(uconvert(u, x)) ustrip(x::Gain) = x.val +ustrip(u::MixedUnits, x::Gain) = ustrip(uconvert(u, x)) isrootpower(y::IsRootPowerRatio{T}) where {T} = T isrootpower(y) = isrootpower_dim(dimension(y)) From 71f4200ee636f43f6bbe7945f76cd9bf38ab8aca Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Mon, 2 May 2022 17:36:43 +0200 Subject: [PATCH 2/3] Add tests --- test/runtests.jl | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index b3afd6c9..4adf33c6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -13,7 +13,7 @@ import Unitful: ms, s, minute, hr, d, yr, Hz, J, A, N, mol, V, mW, W, - dB, dB_rp, dB_p, dBm, dBV, dBSPL, Decibel, + dB, B, dB_rp, dB_p, dBm, dBV, dBμV, dBSPL, Decibel, Np, Np_rp, Np_p, Neper, C @@ -103,6 +103,8 @@ end @test @inferred(ustrip(3*FixedUnits(m))) === 3 @test @inferred(ustrip(3)) === 3 @test @inferred(ustrip(3.0m)) === 3.0 + @test @inferred(ustrip(3.0dB)) === 3.0 + @test @inferred(ustrip(3.0dBm)) === 3.0 # ustrip with type and unit arguments @test @inferred(ustrip(m, 3.0m)) === 3.0 @test @inferred(ustrip(m, 2mm)) === 1//500 @@ -116,6 +118,9 @@ end @test @inferred(ustrip(Int, mm, 2.0m)) === 2000 @test @inferred(ustrip(Float32, NoUnits, 5.0u"m"/2.0u"m")) === Float32(2.5) @test @inferred(ustrip(Int, NoUnits, 3.0u"m"/1.0u"cm")) === 300 + @test @inferred(ustrip(B, 3.0dB)) === 0.3 + @test @inferred(ustrip(dBμV, 3.0dBV)) === 123.0 + @test_throws DimensionError ustrip(dBm, 3.0dBμV) # convert @test convert(typeof(1mm/m), 3) == 3000mm/m @test convert(typeof(1mm/m), 3*NoUnits) == 3000mm/m From 0daf2a63560ab709fdc015532169386b47329f6d Mon Sep 17 00:00:00 2001 From: David Gustavsson Date: Mon, 2 May 2022 18:05:07 +0200 Subject: [PATCH 3/3] dB/m --- src/logarithm.jl | 3 +-- test/runtests.jl | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/logarithm.jl b/src/logarithm.jl index 508958bb..0003e021 100644 --- a/src/logarithm.jl +++ b/src/logarithm.jl @@ -160,9 +160,8 @@ function uconvert(a::MixedUnits{Gain{L1,S1,<:Real}}, x::Level{L2,S2}) where {L1, end ustrip(x::Level{L,S}) where {L<:LogInfo,S} = tolog(L,S,x.val/reflevel(x)) -ustrip(u::MixedUnits, x::Level) = ustrip(uconvert(u, x)) ustrip(x::Gain) = x.val -ustrip(u::MixedUnits, x::Gain) = ustrip(uconvert(u, x)) +ustrip(u::MixedUnits, x) = ustrip(uconvert(u, x)) isrootpower(y::IsRootPowerRatio{T}) where {T} = T isrootpower(y) = isrootpower_dim(dimension(y)) diff --git a/test/runtests.jl b/test/runtests.jl index 4adf33c6..b9a51887 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -121,6 +121,7 @@ end @test @inferred(ustrip(B, 3.0dB)) === 0.3 @test @inferred(ustrip(dBμV, 3.0dBV)) === 123.0 @test_throws DimensionError ustrip(dBm, 3.0dBμV) + @test @inferred(ustrip(B/m, 1.0dB/mm)) ≈ 100.0 # convert @test convert(typeof(1mm/m), 3) == 3000mm/m @test convert(typeof(1mm/m), 3*NoUnits) == 3000mm/m