Repository navigation
fix: assign a bare number to a decibel quantity as decibels - #436
Merged
Merged
Conversation
Construct the canonical dimensionless scalar with the destination numerical scale before converting it. Decibel assignment then agrees with scalar value semantics while percent normalization and typed linear-ratio conversion retain their existing behavior.
nholthaus
force-pushed
the
fix/decibel-assignment-means-decibels
branch
from
October 18, 2026 19:18
7c19685 to
4b67938
Compare
nholthaus
enabled auto-merge (squash)
October 18, 2026 19:21
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Scalar assignment currently treats a bare number assigned to a decibel gain as a linear ratio. This makes
gain = 100.0produce 20 dB even though construction, scalar extraction, and scalar comparison use 100 dB. This change makes scalar assignment preserve the destination's numerical scale while retaining dimensionless normalization and typed quantity conversion.Contract and evidence
The invariant is that scalar assignment agrees with the quantity's public scalar value and scalar comparison. It is not a blanket rule that assignment must equal scalar construction for every unit.
The existing discrepancy breaks ordinary generic code:
After
gain = 100.0,gain == 100.0was false. Likewise, assigningstatic_cast<double>(gain)back to a 3 dB gain changed it to approximately 4.771 dB. These are observable value-contract failures, not just surprising internal storage.std::assignable_from<decibels<double>&, double>passes the syntactic concept checks, but the old operation fails the semantic postcondition that the assigned object compares equal to the source. The compiler does not check that semantic requirement. See the assignment concept contract.Decision and compatibility
The strongest alternative is to define every bare scalar assignment as a canonical linear dimensionless ratio. That interpretation is coherent in isolation and has a long history here: older code assigned directly into linearized storage, and the 2022 concentration-normalization change explicitly used a linear intermediate. Its age does not reconcile it with the rest of the public scalar interface.
Making assignment-as-linear-ratio agree with the public scalar value would instead require changing established scalar extraction and comparison semantics. Correcting assignment is the narrower correction and also agrees with existing decibel construction. This is an intentional behavior correction, with a real compatibility cost:
gain = 100.0gain = 0.0gain = -3.0gain = dimensionless<double>(100.0)percentage = 0.5Code that intends to assign a linear ratio should make that intent explicit:
gain = dimensionless<double>(ratio);Typed quantities retain their physical meaning; their assignment overload and conversion path are unchanged.
Percent is the important boundary on the rule.
percent<double>(0.5)constructs 0.5%, while assigning0.5produces 50%. That distinction is deliberate: percent's public scalar value/comparison is the normalized fraction, so its existing assignment already satisfies the chosen invariant. This change preserves the existing linear-scale normalization path.Smallest implementation
Change one template argument in the existing scalar assignment operator: construct the canonical dimensionless intermediate with
NumericalScaleinstead of hard-codedlinear_scale. Keep the existingunits::convertcall to normalize the destination's conversion factor.This uses the same general path for every scale. Assigning through the destination constructor directly would lose normalization for scaled dimensionless types. Branching on exact equality with
linear_scalewould also mishandle inherited linear scales. The tests cover both a logarithmic percent and an inherited linear percent to guard those boundaries. No new helper, branch, or public API is needed.The scope is scalar assignment. There are broader existing mixed-type equality inconsistencies: a 3 dB quantity and a linear dimensionless quantity of 3 can each compare equal to the scalar 3 while comparing unequal to each other. This change does not claim to resolve the entire scalar interface; it corrects assignment without redesigning those contracts.
Validation
Local validation of this revision:
-Wall -Wconversion -Wshadow -Werror(third-party GoogleTest headers treated as system headers for Clang).git diff --checkis clean.Remote CI is reported separately by the checks on this PR.