Skip to content
This repository was archived by the owner on Aug 6, 2026. It is now read-only.

Repository files navigation

Hensel Code System

Exact Rational Arithmetic via p-adic Numbers — Zero Floating-Point Errors

Demo Python JavaScript License

"Every rational operation yields a result whose prime factorization is exactly the Minkowski sum of the operand lattices."

Why This Exists

Classic floating-point failure: 0.1 + 0.2 = 0.30000000000000004 — wrong.

This caused:

  • 28 deaths (Patriot missile, 1991 — time drift from binary rounding)
  • $370M loss (Ariane 5, 1996 — float-to-integer overflow)
  • Billions in financial errors (Excel 2007 850*77.1 = 100000 bug)

Hensel codes solve this by representing every rational number exactly — using century-old mathematics (Ostrowski 1916, Hensel 1904) and zero new hardware.

How It Works

1. p-adic Encoding (Hensel's Lemma)

Every rational a/b (with p ∤ b) has a unique encoding as a single integer modulo pᵏ:

H(a/b) = a · b⁻¹ (mod pᵏ)

2. Exact Arithmetic

All four operations are integer operations modulo pᵏzero rounding error:

H(a/b) + H(c/d) ≡ H(a/b + c/d)   (mod pᵏ)   ← EXACT
H(a/b) × H(c/d) ≡ H(a/b × c/d)   (mod pᵏ)   ← EXACT

3. Rational Recovery

The Extended Euclidean Algorithm recovers the exact rational from its Hensel code:

Given H and (p, k), recover a/b such that |a|,|b| ≤ √(pᵏ/2)

4. Hierarchical Organization

The Bruhat-Tits tree provides an ultrametric on p-adic numbers — every finite hierarchical clustering (dendrogram) embeds naturally.

Quick Start

Python

from src.hensel_system import HenselCode

# Encode 0.1 exactly
h = HenselCode.from_rational(1, 10, p=7, k=30)

# Exact arithmetic
h1 = HenselCode.from_rational(1, 10, 7, 30)   # 0.1
h2 = HenselCode.from_rational(2, 10, 7, 30)   # 0.2
h3 = h1 + h2                                    # 0.3 — EXACT!

# Recover as rational
num, den = h3.to_rational()                     # (3, 10)
print(f"{num}/{den} = {num/den}")               # 3/10 = 0.3

Install & Run

# No dependencies — pure Python standard library
python src/hensel_system.py

Web Demo

Open index.html in your browser, or visit the live demo at: hensel-code.pages.dev

Theoretical Foundation

Layer Theory Year Provides
Foundation Ostrowski's Theorem 1916 ℚₚ and ℝ are the ONLY completions of ℚ
Encodability Hensel's Lemma 1904 Constructive lifting: every a/b has a unique p-adic expansion
Encoding Krishnamurthy 1977 Hensel codes: encode rationals as integers mod pᵏ
Hierarchy Bruhat-Tits Tree 1970s Ultrametric tree: natural clustering of p-adic numbers

Project Structure

hensel-system/
├── index.html              ← Interactive web demo (Cloudflare Pages entry)
├── demo/
│   ├── demo.js             ← JavaScript HenselCode implementation
│   └── style.css
├── src/
│   └── hensel_system.py    ← Python implementation (518 lines, zero deps)
├── tests/
│   └── test_hensel.py      ← Test suite
├── docs/
│   └── theory.html          ← Full theory documentation
└── README.md

Constraints

What Status
Rationals (a/b, b coprime to p) ✅ Exact
Irrationals (π, e, √2) ⚠️ Requires symbolic representation
Addition of large prime powers ⚠️ Computational bottleneck (factorization)
New hardware required ❌ None — uses standard integer arithmetic
External libraries ❌ None — pure standard library

Practical guidance: Choose p larger than any expected denominator (e.g., p=257 or p=65537).

Real-World Impact

Incident Cause Hensel Fix
Patriot missile (1991) Binary 0.1 rounding drift Exact rational accumulation
Ariane 5 (1996) Float→int overflow No float anywhere in pipeline
Excel 2007 bug FP display error Exact rational intermediate
HFT rounding losses Sub-penny rounding Zero-rounding exact arithmetic

License

MIT — see LICENSE file.


pdf-builder Skill Fix (v1.5)

The \mathbb{...} (blackboard bold) LaTeX command is not supported by matplotlib's mathtext parser. The pdf-builder skill (build_pdf.py) now includes a pre-processor fix that auto-converts \mathbb{...} to \mathbf{...} which renders correctly:

# In build_pdf.py _LATEX_PREPROC_PATTERNS:
(re.compile(r'\\mathbb\{'), r'\\mathbf{'),

This fix is deployed to:

  • Local: ~\.deepchat\skills\pdf-builder\scripts\build_pdf.py
  • R2: qnfo/tools/build_pdf.py
  • Reference: tools/build_pdf_fixed.py (this repo)

Bootstrap Protocol

If the local build_pdf.py is missing:

# Pull from R2 (--remote required for wrangler v4+)
npx wrangler r2 object get qnfo/tools/build_pdf.py --remote --file=_build_pdf.py

# Or get the reference copy from this repo
cp tools/build_pdf_fixed.py _build_pdf.py

Built on mathematics that predates computers themselves. The question is not "if" this is possible, but "when" we decide rounding errors cost more than switching.

About

Exact rational arithmetic via p-adic numbers. Zero floating-point errors. Based on Ostrowski (1916), Hensel (1904), Krishnamurthy (1977). Interactive web demo included.

Topics

Resources

Code of conduct

Contributing

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Used by

Contributors

Languages