Unitful.jl icon indicating copy to clipboard operation
Unitful.jl copied to clipboard

dBW missing, attempts to define it fail.

Open MattEttus opened this issue 2 years ago • 4 comments

In RF, we use dBW sometimes and not just dBm. Attempting to define it fails as follows:

julia> @logscale dBW "dBW" dBW 30u"dBm" 10 false ERROR: invalid redefinition of constant dBW

I also tried the following and got an even weirder message:

julia> @unit dBW "dBW" dBW 30u"dBm" false ERROR: DimensionError: mW² and 1000.0 mW are not dimensionally compatible.

MattEttus avatar Nov 17 '22 05:11 MattEttus

The way to do this is with @logunit, see src/pkgdefaults: https://github.com/PainterQubits/Unitful.jl/blob/ebad4497360693722c9dae0a6d6f28b5647ad4a6/src/pkgdefaults.jl#L673-L681

For dBW, this would be

@logunit dBW "dBW" Unitful.Decibel 1u"W"

Maybe we should just add dBW to Unitful?

sostock avatar Nov 17 '22 08:11 sostock

Thanks. It would be great if dBW was added to the list.

MattEttus avatar Nov 17 '22 16:11 MattEttus

Actually, that doesn't fully work. It lets me use the constant dBW, but not the text u"dBW", and that is necessary for the compound units like dBW/m^2/MHz.

julia> using Unitful julia> @logunit dBW "dBW" Unitful.Decibel 1u"W" julia> power = -100u"dBW" ERROR: LoadError: ArgumentError: Symbol dBW could not be found in unit modules Module[Unitful]

MattEttus avatar Nov 17 '22 16:11 MattEttus

To use a unit with the u"…" macro, you need to register the module that defines the unit:

Unitful.register(YourModule)

If you define the unit in the REPL, YourModule would be Main.

If you define the unit in a package, you should call Unitful.register in the module’s __init__() function:

function __init__()
    Unitful.register(YourModule)
end

(see the documentation on extending Unitful)

sostock avatar Nov 18 '22 13:11 sostock