Repository navigation
Conversation
This branch has not been deployed
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.
An integral temperature comparison must include the datum: 0 Celsius equals 32 Fahrenheit and exceeds 273 kelvin. Compare these coordinates exactly without truncating their fractional offsets or rounding wide integer values.
Addresses the rational/affine portion of #448. Pi-dependent differences remain within that issue's scope.
Before and after
celsius<int>(0) == kelvin<int>(0)truefalsecelsius<int>(0) == fahrenheit<int>(32)falsetruecelsius<int>(0) > kelvin<int>(273)falsetrueThese examples are durable assertions in
IntegerComparison.affineCoordinatesin the existingtest/main.cpp.Contract and decision
Correctness means equality and ordering of the represented physical quantities: linearized value, rational scale, pi exponent, and datum together. This change applies when both operands have integral representations and either both pi exponents are zero, or the pi exponents and datums agree. In the latter case, the equal datums subtract away and the shared positive pi factor cancels, so no pi approximation is needed.
Restricting the original ratio shortcut alone would still allow the integral common-unit conversion to discard fractional datums. Converting every integer operand to floating point would lose adjacent wide integers and small offsets at large magnitudes. Exact bounded rational arithmetic preserves both distinctions.
Implementation
Identical physical mappings use direct signedness-safe integer comparison. Other comparisons in the exact domain clear the original denominators of
value * ratio + datuminto private fixed-size positive and negative accumulators. Their bound follows from the raw representation andintmax_twidths. This avoids signed overflow and overflowing intermediatestd::ratiotypes, without allocation, a dependency, or a public API.The comparator reads already-linearized storage, so a custom numerical scale is neither applied twice nor required to accept a floating representation. Different dimensions remain rejected.
All other comparisons use the baseline comparator implementation, including floating operands and pi-dependent differences. The boundary is protected by cases for very large and very small pi exponents, irrational-versus-rational equality, and constexpr fractional-pi comparisons. Floating approximation is unsuitable for an exact equality decision: distinct quantities can round to the same number, overflow together, or underflow together.
Compatibility and limits
There is no source migration for valid comparisons. Incorrect rational comparison results deliberately change. Storage selection, result-unit naming, conversions, and arithmetic operators are unchanged. Wrapper preconversion and result representation belong to #450 and #432; comparison cannot recover a value discarded before it reaches this function.
This MR does not repair general pi-dependent comparisons, including
degrees<int>(180)versusradians<int>(3). That requires a separate exactness contract and implementation. Issue #448 must remain open for that work. The bounded rational helper is limited to platforms whose standarduintmax_thas at most 64 value bits; raw GNU 128-bit representations retain their complete width.All added tests use existing sources. The integral-default configuration uses component headers because the umbrella header's floating physical constants require a floating default representation.
Validation
Evidence for commit
8d7a1f5on Linux:test/main.cpppass, including 17 focused comparison tests. No sanitizer diagnostics occur.3a7dd0fand passes with this change. The baseline also fails the three temperature examples above.