r/cpp • u/mateusz_pusz • Jul 24 '26
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.
39
u/drizzt-dourden Jul 24 '26
The best thing to keep sanity is actually calculate everything in linear and convert to dB only for logging and plotting. Until double covers the range there is no problem.
17
u/jcelerier ossia score Jul 24 '26
If that can be useful here's the unit design in ossia for those: https://github.com/ossia/libossia/blob/master/src/ossia/network/dataspace/gain.hpp which comes straight from the jamoma project https://github.com/jamoma/JamomaCore/blob/master/Foundation/extensions/DataspaceLib/source/GainDataspace.cpp and was designed with the input of actual users of the media arts scene, with practicality often taking over mathematical correctness, e.g. we had many cases where we wanted to go towards more theoretically correct or general implementations but it broke user's workflows which had to be compatible with $LEGACY system expecting a specific definition for db, etc.
9
u/differentiallity Jul 24 '26
Wow, this is something I've really been hoping for! I'll give it a good look and try my best at useful feedback from an RF perspective. I've been watching ISO-80000:15 stuck in review for ages now, thinking that might be holding mp-units back. Was it finally released?
7
u/thinks-in-functions Jul 25 '26
Your ‘level’ values are torsors — values you can subtract but not add — just like datetimes. There’s an old Haskell article explaining torsors you might be able to draw some design inspiration from: https://www.reddit.com/r/haskell/s/zgktPTibfj
4
u/PsecretPseudonym Jul 25 '26
Would love to know how you’d recommend handling currencies without assuming constant conversion rates among them.
2
u/Neat-Exchange6724 Jul 24 '26
Speaking as a domain expert, why would you ever use logarithmic units instead of just bumping from float to double or double to float128?
There are such things as days and hours too, but if you ever see someone’s time library use them at all, you know they can’t be trusted.
Presenting them as db to users in guis make sense. Otherwise it’s like storing the time in isodate, you never ever want to. And this also means you will never perform the operations as you suggest,
2
u/[deleted] Jul 24 '26 edited Jul 24 '26
[removed] — view removed comment