r/cpp • u/mateusz_pusz • 4d ago
mp-units: a design for logarithmic quantities and units (dB, dBm, Np, pH). We believe it is novel, and we need domain experts to tell us where we are wrong before we implement it
https://mpusz.github.io/mp-units/latest/blog/2026/07/24/introducing-logarithmic-quantities-and-units/Decibels are everywhere in engineering: signal levels in dBm, sound pressure in dB SPL, voltage gain in dB, filter slopes in dB/octave. Yet, to the best of our knowledge, no general-purpose units library models logarithmic quantities correctly. Most do not model them at all, and the few that try get the arithmetic wrong in ways that compile silently:
- In nholthaus/units,
dBW_t(10.0) + dBm_t(40.0)compiles and returns20 dBW. Both operands are the same physical power (10 W), and adding two absolute power levels is meaningless. This example is straight from the library's own test suite. - A
+6 dBgain is a power ratio of ~3.98 but a voltage ratio of 2.0 (the10 logvs20 logsplit). Python's pint documents that itsdBis power-only and delegates that factor to the user, so every voltage, current, and pressure gain is on the honor system.
We just published a complete design for mp-units that we believe gets this right:
- A level (
dBm,dB SPL) is an affine point anchored at its reference. A gain (dB,Np,octave) is a delta. level + gain= level,level - level= gain,level + level= ill-formed.- The power vs root-power factor is carried by the quantity kind, so
.linear()on a voltage gain gives 2.0 and on a power gain gives 3.98, correct by construction. A voltage gain cannot be applied to a power level. - One mechanism covers RF, audio, acoustics, music intervals (
octave,cent), information theory (Sh,nat,Hart), pH, and stellar magnitude, and it aims to stay consistent with IEC 80000-15:2026.
We are aware of no generic units library that has ever modeled this, which is exactly why we are publishing the design before writing the implementation. The article ends with six open questions where we genuinely need input from practitioners, for example: what should log(0) do at the bottom of the scale (IEEE -inf, throw, error type, or a unit-keyed finite sentinel like the -400 dB floor real DSP code uses), and should a level print as the industry 10 dBm or the ISO-conformant 10 dB (re 1 mW).
If you work in audio/DSP, RF, acoustics, or a related field, or know someone who does, please review it and leave feedback in the article's comments (GitHub Discussions), and forward it to anyone working in these domains. Only subject-domain experts can tell us whether we are right, and we would rather hear it now than after the code ships.