PainterQubits / Unitful.jl

Physical quantities with arbitrary units
Other
613 stars 112 forks source link

Cant simplify electrical units as expected (S*Ω) #747

Open LKuhrmann opened 1 week ago

LKuhrmann commented 1 week ago

Hi all, I hope this is not a repeat question.

I am trying to do an operation equivalent to this, but it does not work: uconvert(u"1/km", 1.0u"S" * 1.0u"Ω" * 1.0u"1/km")

MethodError: no method matching uconvert(::Unitful.Quantity{Int64, 𝐋^-1, Unitful.FreeUnits{(km^-1,), 𝐋^-1, nothing}}, ::Unitful.Quantity{Float64, 𝐋^-1, Unitful.FreeUnits{(km^-1, Ω, S), 𝐋^-1, nothing}})

However, similar situations where units cancel each other out work just fine: uconvert(u"Ω/km",1.0*u"1/km" * 1.0u"V"/1.0u"A")

1.0 Ω km^-1

Is this a bug or am I misunderstanding what this package can do? Should I be using a different function?

Thanks!

sostock commented 1 week ago

This doesn’t work because u"1/km" is a quantity, not a unit. You can use u"km^-1" instead.

Duplicate of #720.

LKuhrmann commented 1 week ago

I see, that wasn't obvious to me. I thought only if there is something before the u Unitful creates a quantity, but I misunderstood.

Maybe an Error message like "Can not unconvert a quantity to another quantity, only to a unit." would be helpful? In my opinion the current error message is not intuitive.

Maybe somthing like the following could be added to uconvert in conversion.jl:

function uconvert(x::Quantity{T,D,U}, y::Quantity{T,D,U}) 
    error("Can not unconvert a quantity to another quantity, only to a unit.")
end

What do you think? Either way, thanks a lot for the help!