fix: improve efficiency and accuracy of value_cast for quantity_point#765
Merged
fix: improve efficiency and accuracy of value_cast for quantity_point#765
Conversation
Rewrite the quantity_point overload of sudo_cast to pick the best intermediate unit/rep for each conversion case: - Same origin: delegate entirely to the quantity sudo_cast (unchanged). Also fix a pre-existing bug where the result was incorrectly tagged with FromQP::point_origin instead of ToQP::point_origin. - Same unit, different origin: skip unit scaling altogether; compute the origin offset in the common rep and add it directly. - Different unit, floating-point rep: pick the larger of the two units as the intermediate (to minimise the scaling magnitude), then use point_for() to let the library's common-unit arithmetic handle the origin shift — this avoids the binary FP precision loss that occurs when the offset is expressed explicitly in the larger unit. - Different unit, integer rep: prefer the output unit as intermediate so no extra scaling is needed after the offset addition. If the origin offset would overflow the output-unit type, fall back to the input unit where it is guaranteed to fit. Also expand the value_cast tests for quantity_point to cover every branch explicitly, including the new integer overflow-fallback path. Co-authored-by: Yves Delley <burnpanck@users.noreply.github.com> Closes: #598
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.
Overview
Rewrites the
quantity_pointoverload ofdetail::sudo_castto select the optimal intermediate unit and representation type for each conversion case, avoiding unnecessary operations and improving numerical accuracy.This implements the design from #598 (originally authored by @burnpanck). The commit carries a
Co-authored-bytrailer to credit the original work. PR #598 has been closed in favour of this one.Problem
The previous implementation always delegated cross-origin conversions through a recursive
point_for()call (twice for the small-unit case), unconditionally rescaling to a common type before adding the origin offset. This forces extra arithmetic operations in cases where none are needed, and in the floating-point path could accumulate rounding error by expressing the offset in the wrong unit.Additionally, there was a latent bug in the same-origin branch: the returned
quantity_pointwas tagged withFromQP::point_origininstead ofToQP::point_origin.Solution — four branches
quantitysudo_cast. Also fixes the origin tag bug.point_for()to let the library's common-unit arithmetic add the offset. This avoids precision loss that would occur if the offset were expressed explicitly in the larger unit (e.g. 42 m → 0.042 km loses bits).Tests
The existing
value_casttests forquantity_pointare expanded to exercise every branch:km→mtest (from-unit larger); newm→kmtest (to-unit larger).int8_t mm → cmpair.int16_t m → mmtest where the 42 000 mm offset overflowsint16_t, so the input unit (m) is used instead.Related
Closes #598