t81-foundation

T81 Foundation — Scientific Investigation 02

Exact Rational Conversion Feasibility Report

Repository: https://github.com/t81dev/t81-foundation Evaluated Revision: 17f835453c1e16639ad7b9bff87bd15225e8912d Role: Independent Numerical Computing Researcher Research Phase: Gate 1B — Exact Conversion and Rounding Semantics Final Disposition: REVISE


Executive Summary

This investigation evaluates whether exact rational arithmetic provides a mathematically correct, deterministic, cross-platform conversion from IEEE 754 binary64 floating-point numbers to T81 balanced ternary floating-point representations (T81Float<M,E>) without relying on host-dependent floating-point mathematics or libm transcendental functions.

The previous Gate 1 assessment identified three counterexamples (0x7fefffffffffffff, 0x0000000000000001, and 0x3fe0000000000000) where the production C++ T81Float<27,9>::from_double implementation produced incorrect, zeroed, or host-divergent outputs.

This investigation independently reproduced all three counterexamples using exact rational arithmetic, diagnosed the root causes in the production C++ code, validated the reference Python implementations (oracle.py and reference.py), established a conventional exact-arithmetic baseline, analyzed computational complexity, and identified key specification ambiguities.

The final disposition is REVISE: The mathematical approach using exact rational conversion is sound, host-independent, and fully deterministic. However, production implementation MUST NOT proceed until normative specification conflicts (spec/t81-data-types.md §2.3 vs. T81Float.hpp), public API signatures (Result<T,E> vs. noexcept), default conversion modes (exact_only vs. nearest_even), and RFC-00D5 / ADR-0005 draft contracts are formally resolved and approved by project governance.


Section 1: Evaluated Repository Revision & Evidence Inventory

1.1 Evaluated Repository Revision

1.2 Evidence Inventory

  1. Production Source Code:
    • include/t81/types/T81Float.hpp: C++ template class for balanced-ternary floating-point numbers, including from_double() and to_double() methods.
  2. Normative Specifications & Architectural Documents:
    • spec/t81-data-types.md §2.3: Normative specification for T81Float.
    • docs/research/NUMERICAL_SCIENTIFIC_ASSESSMENT.md: Gate 1 Numerical Scientific Assessment.
    • spec/rfcs/RFC-00D5-checked-floating-point-conversion.md: Draft RFC proposing checked floating-point conversion.
    • docs/architecture/adr/ADR-0005-checked-float-conversion.md: Draft Architecture Decision Record for checked conversion.
  3. Audit Artifacts (experiments/float_conversion_audit/):
    • probe.cpp: C++ test adapter probing production from_double() behavior across O0/O3 optimization levels and host long double configurations.
    • oracle.py: Standalone Python exact rational oracle using fractions.Fraction.
    • test_oracle.py: Unit test suite verifying oracle.py against exact binary64 decomposition and brute-force <4,4> enumeration.
    • audit.py: Platform audit driver script generating JSON evidence.
    • Native JSON evidence records (native-ci.json, native-macos-arm64.json).
  4. Checked Contract Reference Artifacts (experiments/checked_float_contract/):
    • reference.py: Executable reference implementation of proposed RFC-00D5 contract supporting exact-only and nearest-even modes using Python Fraction.
    • test_reference.py: Reference test runner evaluating static corpus and tie/binade edge cases.
    • corpus.json: 104 machine-readable static test cases with explicit inputs and expected outputs.
    • PLAN.md: Draft execution and approval roadmap for checked float conversion.

Section 2: Precise Mathematical Conversion Contract

2.1 Representation Value Equation

A valid finite T81Float<M,E> encoding consists of:

The exact mathematical value represented by a finite tuple $(S, E_{\text{unb}}, M_{\text{int}})$ is: \(V(S, E_{\text{unb}}, M_{\text{int}}) = S \cdot M_{\text{int}} \cdot 3^{E_{\text{unb}} - (M - 1)}\)

2.2 Finite Representable Range and Precision (<27, 9>)

For standard canonical format $M = 27$ and $E = 9$:

Representable Bounds:

Comparison with IEEE 754 Binary64: IEEE 754 binary64 has a finite positive range of approximately $[4.94 \times 10^{-324}, 1.798 \times 10^{+308}]$. Because $10^{-4707} < 10^{-324}$ and $10^{+4695} > 10^{+308}$, every finite binary64 value falls strictly within the representable range of T81Float<27,9>. Range overflow or underflow never occurs when converting finite binary64 numbers to T81Float<27,9>.

2.3 Encoding Uniqueness and Canonicality

2.4 Required Rounding Mode and Tie-Breaking

2.5 Required Special Value Behavior

2.6 Specification vs. Implementation Discrepancies

  1. API & Error Handling: spec/t81-data-types.md §2.3 requires explicit deterministic error codes or Result[T,E] for precision loss or invalid states. include/t81/types/T81Float.hpp provides from_double(double) -> T81Float as a noexcept function that silently rounds or returns NaE/Inf sentinels.
  2. Deterministic Arithmetic Boundary: The specification mandates that conversions and arithmetic are strictly deterministic software implementations. T81Float::from_double uses host C++ <cmath> functions (std::log, std::floor, powl, std::llround), introducing host compiler, platform, and floating-point environment dependencies.
  3. Data Model: The spec describes Float components as arbitrary-precision T81BigInt, whereas the C++ implementation uses fixed compile-time template bitfields (T81Int<1+E+M>).

Section 3: Independently Reproduced Counterexamples

The three binary64 bit patterns identified in Gate 1 were independently decoded into exact rational numbers $P/Q$, evaluated against candidate ternary encodings, and compared against production C++ execution on Linux x86-64 (GCC 13.3) and macOS ARM64 (Apple Clang 16.0).

Case Binary64 Bits Classification
1. Maximum finite positive 0x7fefffffffffffff $(2^{53}-1) \times 2^{971}$
2. Minimum positive subnormal 0x0000000000000001 $1 / 2^{1074}$
3. Positive one-half 0x3fe0000000000000 $1 / 2$

Case 1: Maximum Finite Positive (0x7fefffffffffffff)


Case 2: Minimum Positive Subnormal (0x0000000000000001)


Case 3: Positive One-Half (0x3fe0000000000000)


Section 4: Exact Expected and Observed Outputs

Test Input Input Binary64 Hex Observed C++ Production Output (from_double) Exact Rational Reference Expected Output (nearest_even) Audit Outcome
Max finite positive 7fefffffffffffff +0E0 ++0+-+-00+--+-0+--+0+---000+E646 INEXACT / DEFECT
Min positive subnormal 0000000000000001 +0E0 (macOS) / ++----...E-677 (Linux) ++----00++++-0--00-+++0++000E-677 INEXACT / DIVERGENT
Positive 0.5 3fe0000000000000 ++--------------------------E0 ++++++++++++++++++++++++++++E-1 INEXACT / SUBOPTIMAL
Positive Zero 0000000000000000 +0E0 +0E0 EXACT / PASS
Negative Zero 8000000000000000 +0E0 +0E0 (flag: negative_zero_normalized) EXACT / PASS
Positive Infinity 7ff0000000000000 +Inf nonfinite_input / invalid INVALID / PASS
Quiet NaN 7ff8000000000000 NaE nonfinite_input / invalid INVALID / PASS

Section 5: Reference-Oracle Validation

Two Python reference implementations were evaluated:

  1. experiments/float_conversion_audit/oracle.py (Audit Oracle)
  2. experiments/checked_float_contract/reference.py (RFC-00D5 Draft Reference)

5.1 Validation Results


Section 6: Conventional Baseline Comparison

A conventional exact-arithmetic reference was constructed using Python fractions.Fraction (and equivalent GNU MP mpq_t C++ design).

6.1 Determinism Analysis: Ternary vs. Conventional


Section 7: Algorithmic Complexity and Measured Costs

7.1 Complexity Analysis

7.2 Measured Benchmark Costs


Section 8: Remaining Specification Ambiguities

Before production implementation can begin, the following ambiguities MUST be resolved:

  1. Public API Error Handling Signature: Should conversion return T81Result<T81Float> / Result[T,E] (as required by RFC-00D5 and spec/t81-data-types.md §2.3), or maintain a non-throwing sentinel-returning noexcept signature?
  2. Default Conversion Mode: Should the default conversion function be exact_only (failing on inexact binary64 inputs) or nearest_even (performing implicit lossy rounding)?
  3. Data Model Synchronization: Should spec/t81-data-types.md §2.3 be updated to reflect fixed-width C++ template storage T81Float<M,E>, or should T81Float be refactored to use dynamic T81BigInt components?
  4. Negative Zero Semantics: Is negative zero normalized to $+0\text{E}0$ with a status flag, or should signed zero be preserved in internal storage?

Section 9: Final Disposition

Disposition: REVISE

Rationale:

  1. Mathematical Soundness: The proposed exact rational conversion algorithm (quantize_rational / reference.py) is mathematically correct, host-independent, bit-exact, and fully deterministic across all platform architectures.
  2. Specification & API Unreadiness: Production implementation MUST NOT proceed because normative specifications (spec/t81-data-types.md §2.3), RFC-00D5, ADR-0005, and public API signatures (Result[T,E] error propagation) remain unmerged drafts with unresolved ambiguities.
  3. Production Isolation: Production code (include/t81/types/T81Float.hpp) remains unchanged and preserved in its quarantined state, preventing premature changes before governance approval.

Reproduction Commands

To verify the evidence and test oracles:

# 1. Run exact audit reference tests
python3 experiments/float_conversion_audit/test_oracle.py

# 2. Run RFC-00D5 draft checked contract reference tests
python3 experiments/checked_float_contract/test_reference.py

# 3. Probe production C++ implementation counterexamples
g++ -std=c++23 -O0 -DNDEBUG -DT81_DETERMINISTIC -I include \
  experiments/float_conversion_audit/probe.cpp -o /tmp/probe_test
/tmp/probe_test << 'EOF'
7fefffffffffffff
0000000000000001
3fe0000000000000
EOF

Section 10: Falsifiable Follow-Up Experiment (Gate 1B Closure)