Unitful quantities of something

Say I want to represent concentrations like “milligrams of salt per milligrams of sugar”. Unitful.jl doesn’t seem to support this:

julia> typeof(3u"mg"/100u"mg")

What would you suggest?

maybe Defining new units · Unitful.jl ?

Following that example I can get

julia> 3u"myMeter" / 2u"m"
1.5 m m^-1

julia> 3u"myMeter" / 2u"m" |> typeof
Quantity{Float64, NoDims, Unitful.FreeUnits{(m^-1, m), NoDims, nothing}}

julia> 3u"myMeter" / 2u"m" == 3u"m" / 2u"myMeter" 

which is better than nothing but doesn’t really distinguish between the two quantities.

Guess, in order to distinguish between such quantities you need to make them different Dimensions. In the example, you are just defining a different unit of measurement, i.e., myMeter, with a corresponding conversion factor into the standard unit of measurement m. It still applies to the same physical quantity/dimension and thus \frac{3 \; \mathrm{myMeter}}{2 m} is a unitless quantity.
Not sure what the proper physical quantity for salt per sugar would/should be? Maybe better to use standard concentration or mass units and separately track to which substances these apply?


A bit late to the party but maybe @Raf’s UnitfulMoles.jl is helpful?

AFAIK, you can work with custom moles, as below (copy-pasted from the ReadMe):

julia> using UnitfulMoles, Unitful

julia> @mol A

julia> @mol B

julia> 0.5molA/molB
0.5 molA molB⁻¹

but you can’t do the same for other units, like grams. You can maybe create your own package called UnitfulGrams.jl that just replaces UnitfulMoles.jl’s moles for grams? :man_shrugging:

1 Like