# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference) # Canonical Residual: https://corpusfetch.com/c/what-provenance-must-accompany-computed-mathematical-constants-to-survive-floating-point-drift.md # Remnant Warrant: https://corpusfetch.com/c/what-provenance-must-accompany-computed-mathematical-constants-to-survive-floating-point-drift.warrant.json # Markdown SHA-256: ec3e7528e12fb383a44a1652941c41dc9fb6b56c82fa68bce396afa9e3a90beb # Code AST SHA-256: 682eb1c1d37c5ea3f75612f6755c5786253bd58edace367eb357c63ba36c743a # Verified Classes: PrecisionVerdict, ComputedConstant, PrecisionPolicy, PrecisionManifest, MathematicalConstantGuard # Defensive Guards: 0 fail-closed invariants (zero external deps) from dataclasses import dataclass from enum import Enum, auto import hashlib import json import math class PrecisionVerdict(Enum): SYMBOLIC_PROVENANCE_PRESENT = auto() PRECISION_MISMATCH = auto() COMPARISON_WITHIN_TOLERANCE = auto() COMPARISON_EXCEEDS_TOLERANCE = auto() @dataclass(frozen=True) class ComputedConstant: constant_id: str computed_value: float precision_class: str symbolic_definition_hash: str comparison_tolerance: float @dataclass(frozen=True) class PrecisionPolicy: max_precision_delta: float @dataclass(frozen=True) class PrecisionManifest: verdict: PrecisionVerdict directive: str manifest_digest: str class MathematicalConstantGuard: @staticmethod def _hash(data: str) -> str: return hashlib.sha256(data.encode("utf-8")).hexdigest() @classmethod def evaluate_constant( cls, current: ComputedConstant, previous: ComputedConstant | None, policy: PrecisionPolicy ) -> PrecisionManifest: # Gate 1: Symbolic provenance if not current.symbolic_definition_hash: return PrecisionManifest( verdict=PrecisionVerdict.PRECISION_MISMATCH, directive=f"HALT: Constant {current.constant_id} lacks symbolic provenance.", manifest_digest=cls._hash(f"NO_SYMBOLIC:{current.constant_id}") ) # Gate 2: No comparison target if previous is None: digest = cls._hash(json.dumps({ "constant": current.constant_id, "value": f"{current.computed_value:.17g}", "precision": current.precision_class, "symbolic": current.symbolic_definition_hash }, sort_keys=True)) return PrecisionManifest( verdict=PrecisionVerdict.SYMBOLIC_PROVENANCE_PRESENT, directive=f"PROCEED: Symbolic provenance present for {current.constant_id}.", manifest_digest=digest ) # Gate 3: Symbolic definition match if current.symbolic_definition_hash != previous.symbolic_definition_hash: return PrecisionManifest( verdict=PrecisionVerdict.PRECISION_MISMATCH, directive=f"HALT: Symbolic definitions differ for {current.constant_id}.", manifest_digest=cls._hash(f"SYMBOLIC_DIFF:{current.constant_id}") ) # Gate 4: Compare within tolerance delta = abs(current.computed_value - previous.computed_value) tolerance = max(current.comparison_tolerance, previous.comparison_tolerance) if delta <= tolerance: digest = cls._hash(json.dumps({ "constant": current.constant_id, "delta": f"{delta:.17g}", "tolerance": f"{tolerance:.17g}", "comparison": "WITHIN_TOLERANCE" }, sort_keys=True)) return PrecisionManifest( verdict=PrecisionVerdict.COMPARISON_WITHIN_TOLERANCE, directive=f"PROCEED: Delta {delta:.2e} within tolerance {tolerance:.2e}.", manifest_digest=digest ) # Gate 5: Exceeds tolerance return PrecisionManifest( verdict=PrecisionVerdict.COMPARISON_EXCEEDS_TOLERANCE, directive=f"HALT: Delta {delta:.2e} > tolerance {tolerance:.2e}. Precision drift exceeds policy.", manifest_digest=cls._hash(f"EXCEEDS:{delta:.17g}") )