Skip to content

Compare integral affine coordinates exactly - #460

Open
nholthaus wants to merge 2 commits into
mainfrom
codex/integer-comparison-equivalence
Open

nholthaus wants to merge 2 commits into
mainfrom
codex/integer-comparison-equivalence

Conversation

@nholthaus

@nholthaus nholthaus commented Oct 18, 2026 •

Copy link
Copy Markdown
Owner

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

Expression Baseline 3a7dd0f Proposed behavior
celsius<int>(0) == kelvin<int>(0) true false
celsius<int>(0) == fahrenheit<int>(32) false true
celsius<int>(0) > kelvin<int>(273) false true

These examples are durable assertions in IntegerComparison.affineCoordinates in the existing test/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 + datum into private fixed-size positive and negative accumulators. Their bound follows from the raw representation and intmax_t widths. This avoids signed overflow and overflowing intermediate std::ratio types, 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) versus radians<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 standard uintmax_t has 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 8d7a1f5 on Linux:

  • GCC 13.4, GNU C++23, Release: 523 unit tests pass, including Eigen 3.4 coverage; all 13 CTest targets pass. Optional JSON integration is disabled in this configuration.
  • GCC 13.4, strict C++23: all 77 diagnostic cases pass, including integral cross-dimension rejection.
  • Clang 23 with libstdc++13, GNU C++23, undefined-behavior sanitizer with no recovery: all 421 tests in test/main.cpp pass, including 17 focused comparison tests. No sanitizer diagnostics occur.
  • Clang 23 with libc++, strict C++23: the existing lean source builds and runs with ordinary and integral defaults, including the fractional-pi constexpr assertions. The integral-default build reports the pre-existing pi-constant narrowing warning.
  • The same maintained lean affine regression fails against baseline 3a7dd0f and passes with this change. The baseline also fails the three temperature examples above.
  • Independent review verifies that all 43 nonblank lines of the baseline comparator remain unchanged, apart from indentation, in the fallback.

@nholthaus nholthaus changed the title Preserve complete coordinates in integral unit comparisons Compare integral affine coordinates exactly Oct 18, 2026

This branch has not been deployed

No deployments
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.

1 participant