Skip to content

Fix decibel (dB) parsing of logarithmic units (#62)#75

Merged
dalito merged 2 commits into
mainfrom
issue62_db-parsing
Jun 4, 2026
Merged

Fix decibel (dB) parsing of logarithmic units (#62)#75
dalito merged 2 commits into
mainfrom
issue62_db-parsing

Conversation

@dalito

@dalito dalito commented Jun 4, 2026

Copy link
Copy Markdown
Owner

Issue #62 had more layers than expected from the report.

  1. dB → decibyte / dB[W] → decibyte·watt: In UcumToPintTransformer.simple_unit, the prefix branch resolved the bare atom through pint before applying MAPPINGS_UCUM_TO_PINT. Pint reads bare B as byte, so d+byte = decibyte. The B→bel mapping lived only in an unreachable fallback. And even with the mapping, dbel can't work — pint forbids linearly prefixing a logarithmic unit, because decibel is a distinct unit from bel, not a prefixed bel.
  2. dB[mW] parse error: Genuinely correct — UCUM defines only B[W] and B[kW] for power; there is no milliwatt bel. dBm has no UCUM representation. (Locked in by a test; the cryptic message is a separate grammar concern.)
  3. Latent factor-of-10 bug: All bel_* defs used logfactor: 10, making bare B[W] return decibel values (1.259 W instead of the true bel-watt 10 W).
  4. A related issue was caught and corrected: UCUM uses 2 lg (field/amplitude levels: B[V], B[mV], B[uV], B[10.nV], B[SPL]) vs lg (power levels: B, B[W], B[kW,]). So field bels need logfactor 2 and their decibels logfactor 20; power bels logfactor 1 and decibels 10.

Changed files:

  • pint_ucum_defs.txt - corrected bel_* logfactors (1 for power, 2 for field) so bare B[...] are true bels; added decibel_spl/volt/millivolt/microvolt/10nanovolt/kilowatt (logfactor 20/10).
  • ucum_pint.py - added MAPPINGS_UCUM_PREFIXED_TO_PINT (e.g. dB→decibel, dB[W]→decibelwatt) consulted in the prefix branch of both transformers.
  • test_ucum_pint.py - new tests covering decibel atoms, true-bel values, str-transformer consistency, and the invalid dB[mW].

Fixes #62

dalito added 2 commits June 4, 2026 17:46
UCUM "dB" was parsed as deci+byte (decibyte): the prefix branch
resolved atoms via pint before applying the UCUM mapping, and bare
"B" is pint's byte. Logarithmic units also cannot be linearly
prefixed in pint (decibel is a distinct unit from bel), so map the
supported decibel forms (dB, dB[W], dB[V], ...) to pint's
decibel-family units explicitly.

Also correct the bel reference units, which used logfactor 10
(decibel) instead of the UCUM lg/2lg factors (1 for power, 2 for
field levels), so bare B[W] is now a true bel-watt.

Fixes #62
Bump pre-commit-hooks (v5->v6), ruff (v0.11->v0.15) and codespell
(v2.4.1->v2.4.2). Ignore uv.lock as it is not tracked.
@dalito

dalito commented Jun 4, 2026

Copy link
Copy Markdown
Owner Author

Related: #76 (Clearer error message for invalid UCUM units).

@dalito dalito merged commit 370f224 into main Jun 4, 2026
5 checks passed
@dalito dalito deleted the issue62_db-parsing branch June 4, 2026 18:13
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Incorrect parsing of decibel units

1 participant