Research-Stack/4-Infrastructure/lib/q16.py
Devin AI 0639eae30a chore(consolidation): integrate E8Sidon stack (PRs #79 #80 #81 #89) into one PR
Squash the four overlapping feature branches into a single change set against
main, eliminating cross-PR merge conflicts and the duplicated CI-fix scripts.

What this brings in (merge order #79 -> #80 -> #81 -> #89):
- #79 refactor(infra): shared utilities (4-Infrastructure/lib/*: q16, hashing,
  jsonl, fraction_utils) + the scripts/math-first/* validators that the
  math-check CI requires.
- #80 feat(lean): Semantics.E8Sidon (1025 lines) -- Eisenstein coefficient
  identity E4^2 = E8 and the Sidon framework. E4_sq_eq_E8_coeff is fully proved
  (all Fourier-coefficient extraction machine-checked); the single residual gap
  is pinned to E4_sq_eq_E8_qExpansion (Mathlib lacks the valence formula /
  dim M8 = 1). 4 sorries + 1 axiom (e8_additive_completeness), all TODO(lean-port).
- #81 refactor(lean): Float-free FixedPoint core (integer-only sqrt/log2/expNeg).
  E8Sidon.lean kept at #80's final 1025-line version (the #81 intermediate
  438-line copy was overridden by merge order).
- #89 feat(lean): Semantics.RRC.PolyFactorIdentity -- short-sleeve polynomial
  detection at the zerocopy limb boundary; now imports Semantics.E8Sidon for
  sigma3/sigma7/convolutionLHS (single source of truth) instead of inlining them.

Conflict resolution:
- flake.nix -> canonical rs-surface removal (Garnix shutdown).
- scripts/math-first/* -> byte-identical across branches, clean.
- .cursorrules / AGENTS.md -> unified; baselines + sorry inventory refreshed.

Verification:
- lake build (default aggregator): 3573 jobs, 0 errors.
- lake build Semantics.RRC.PolyFactorIdentity (E8Sidon + FixedPoint + PolyFactor):
  3655 jobs, 0 errors. Witnesses verified (sigma7 4 = 16513, convolutionLHS 6 = 2350).
- Python tests: 68/68 pass.

Note: the "Workers Builds: researchstack" check is a preexisting external
Cloudflare build unrelated to this change (no branch touches 4-Infrastructure/cloudflare/).

Build: 3573 jobs (default), 3655 jobs (narrow), 0 errors
Co-Authored-By: Allaun Silverfox <bigdataiscoming+9i37y6j2@protonmail.com>
2026-06-16 02:01:31 +00:00

59 lines
1.5 KiB
Python

"""
Q16_16 fixed-point arithmetic for deterministic compute across all substrates.
All thresholds and metric values are stored as Q16_16 integers.
One = 0x00010000 = 65536. Float is forbidden in compute paths; the
converters here are boundary-only (JSON parsing, display).
"""
from __future__ import annotations
Q16_ONE: int = 0x00010000 # 65536
Q16_HALF: int = 0x00008000
Q16_SCALE: int = 65536
def to_q16(value: float) -> int:
"""Convert a float to Q16_16. Only allowed at the external boundary."""
return int(round(value * Q16_ONE))
def from_q16(value: int) -> float:
"""Convert Q16_16 back to float. Only for display, never in compute."""
return value / Q16_SCALE
def q16_add(a: int, b: int) -> int:
return a + b
def q16_sub(a: int, b: int) -> int:
return a - b
def q16_mul(a: int, b: int) -> int:
"""Multiply two Q16_16 values with normalization."""
return (a * b) // Q16_ONE
def q16_div(a: int, b: int) -> int:
"""Divide two Q16_16 values with normalization."""
if b == 0:
raise ZeroDivisionError("Q16 division by zero")
return (a * Q16_ONE) // b
def q16_gt(a: int, b: int) -> bool:
return a > b
def q16_ge(a: int, b: int) -> bool:
return a >= b
def ratio_q16(numerator: float, denominator: float) -> int:
"""Compute Q16_16 ratio of two floats, clamped to [0, 1]."""
if denominator == 0:
return 0
r = numerator / denominator
r = max(0.0, min(1.0, r))
return int(r * Q16_ONE)