Skip to content

8. Non-linear conversions ​

Factors and offsets cover most conversions, but not all: the power in decibel-milliwatts is a logarithm of the power in milliwatts, PdBm=10log10⁡PmW. For such conversions FURY takes a converter of your own: a type that extends uom_converter with the formulas.

f90
type, extends(uom_converter) :: dbm_to_mw
  contains
    procedure, nopass    :: convert_float128
    procedure, nopass    :: convert_float64
    procedure, nopass    :: convert_float32
    procedure, pass(lhs) :: assign_converter
endtype dbm_to_mw

The formulas convert a magnitude from the unit to its alias, or back with inverse=.true.; there is one for each real kind, so that the same converter serves uom32, uom64 and uom128 (the 128 bits one is required even without quadruple precision). The 64 bits one:

f90
pure function convert_float64(magnitude, inverse) result(converted)
!< P[mW] = 10**(P[dBm]/10), inverse P[dBm] = 10*log10(P[mW]).
real(R8P), intent(in)           :: magnitude
logical,   intent(in), optional :: inverse
real(R8P)                       :: converted
logical                         :: inverse_

inverse_ = .false. ; if (present(inverse)) inverse_ = inverse
if (inverse_) then
  converted = 10._R8P * log10(magnitude)
else
  converted = 10._R8P ** (magnitude / 10._R8P)
endif
endfunction convert_float64

assign_converter copies a converter (see the full program).

Using it ​

The alias @user mW declares a conversion towards mW whose formula is supplied by the program; set_alias_conversion attaches the converter to it, by the index of the reference (1) and of the alias (2, the first is the symbol dBm):

f90
dBm = uom64('dBm = @user mW')
mW  = uom64('mW')
call dBm%set_alias_conversion(reference_index=1, alias_index=2, convert=converter)

power = 20._R8P * dBm
converted = power%to(mW)
print '(A)', power%stringify(format='(F4.1)')//' = '//converted%stringify(format='(F5.1)')
call power%unset
power = converted%to(dBm)
print '(A)', converted%stringify(format='(F5.1)')//' = '//power%stringify(format='(F4.1)')
$ tutorial_8
20.0 dBm = 100.0 mW
100.0 mW = 20.0 dBm

What you learned

A converter extends uom_converter; @user declares the alias, set_alias_conversion attaches the formulas. Reference: Conversions.

Next: 9. A units system of yours.

Released under GPL v3, BSD 2-Clause, BSD 3-Clause or MIT, at your choice.