I think NaturallyUnitful.jl
does not handle fractional powers of units correctly. At least that's what I guess from trying to get the Planck mass in eV
by taking 1/sqrt(8 pi G)
with G
from PhysicalConstants.jl
and then trying to convert this to natural units using NaturallyUnitful.jl
as follows:
julia> using PhysicalConstants.CODATA2018: G
julia> Mpl = 1/sqrt(8pi* G)
24416.151347483574 kg^1/2 s m^-3/2
julia> using NaturallyUnitful
julia> Mpl |> natural
ERROR: InexactError: Int64(-3//2)
Stacktrace:
[1] Integer
@ ./rational.jl:109 [inlined]
[2] convert
@ ./number.jl:7 [inlined]
[3] setindex!(h::Dict{Symbol, Int64}, v0::Rational{Int64}, key::Symbol)
@ Base ./dict.jl:382
[4] setDimPow(D::Dict{Symbol, Int64}, x::Unitful.Dimension{:Length})
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:12
[5] dimDict(x::Unitful.Dimensions{(Unitful.Dimension{:Length}(-3//2), Unitful.Dimension{:Mass}(1//2), Unitful.Dimension{:Time}(1//1))})
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:21
[6] dimDict
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:26 [inlined]
[7] _natural(base::Unitful.FreeUnits{(eV,), 𝐋^2 𝐌 𝐓^-2, nothing}, q::Quantity{Float64, 𝐌^1/2 𝐓 𝐋^-3/2, Unitful.FreeUnits{(kg^1/2, m^-3/2, s), 𝐌^1/2 𝐓 𝐋^-3/2, nothing}})
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:59
[8] #natural#1
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
[9] natural
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
[10] |>(x::Quantity{Float64, 𝐌^1/2 𝐓 𝐋^-3/2, Unitful.FreeUnits{(kg^1/2, m^-3/2, s), 𝐌^1/2 𝐓 𝐋^-3/2, nothing}}, f::typeof(natural))
@ Base ./operators.jl:858
[11] top-level scope
@ REPL[4]:1
A more artificial example using fractional powers is this:
julia> using NaturallyUnitful
julia> unnatural(u"(km/s)^(3/2)", 1)
ERROR: InexactError: Int64(3//2)
Stacktrace:
[1] Integer
@ ./rational.jl:109 [inlined]
[2] convert
@ ./number.jl:7 [inlined]
[3] setindex!(h::Dict{Symbol, Int64}, v0::Rational{Int64}, key::Symbol)
@ Base ./dict.jl:382
[4] setDimPow
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:12 [inlined]
[5] dimDict(x::Unitful.Dimensions{(Unitful.Dimension{:Length}(3//2), Unitful.Dimension{:Time}(-3//2))})
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:21
[6] dimDict
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:26 [inlined]
[7] _natural(base::Unitful.FreeUnits{(eV,), 𝐋^2 𝐌 𝐓^-2, nothing}, q::Quantity{Int64, 𝐋^3/2 𝐓^-3/2, Unitful.FreeUnits{(km^3/2, s^-3/2), 𝐋^3/2 𝐓^-3/2, nothing}})
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:59
[8] #natural#1
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
[9] natural
@ ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:56 [inlined]
[10] unnatural(targetUnit::Unitful.FreeUnits{(km^3/2, s^-3/2), 𝐋^3/2 𝐓^-3/2, nothing}, q::Int64)
@ NaturallyUnitful ~/.julia/packages/NaturallyUnitful/LJau5/src/NaturallyUnitful.jl:87
[11] top-level scope
@ REPL[2]:1
julia> unnatural(u"(km/s)^(4/2)", 1)
8.987551787368178e10 km^2 s^-2
It would be great if such cases worked as well. Otherwise, I find this packge very useful!