valuebridge-tdfloat 1.0.0__tar.gz → 1.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- valuebridge_tdfloat-1.1.0/MANIFEST.in +21 -0
- valuebridge_tdfloat-1.1.0/PKG-INFO +263 -0
- valuebridge_tdfloat-1.1.0/README.md +234 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/pyproject.toml +6 -2
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/__init__.py +2 -1
- valuebridge_tdfloat-1.1.0/valuebridge/tdfloat/ieee754/__init__.py +37 -0
- valuebridge_tdfloat-1.1.0/valuebridge/tdfloat/ieee754/_mpfr_bridge.py +33 -0
- valuebridge_tdfloat-1.1.0/valuebridge/tdfloat/ieee754/_precision.py +3 -0
- valuebridge_tdfloat-1.1.0/valuebridge/tdfloat/ieee754/constants.py +89 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/math.py +6 -7
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/tdfloat.py +27 -13
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge_tdfloat.egg-info/SOURCES.txt +5 -5
- valuebridge_tdfloat-1.0.0/PKG-INFO +0 -192
- valuebridge_tdfloat-1.0.0/README.md +0 -163
- valuebridge_tdfloat-1.0.0/tests/test_tdfloat.py +0 -493
- valuebridge_tdfloat-1.0.0/valuebridge_tdfloat.egg-info/PKG-INFO +0 -192
- valuebridge_tdfloat-1.0.0/valuebridge_tdfloat.egg-info/dependency_links.txt +0 -1
- valuebridge_tdfloat-1.0.0/valuebridge_tdfloat.egg-info/top_level.txt +0 -1
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/LICENSE +0 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/setup.cfg +0 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/_arithmetic.py +0 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/_encoding.py +0 -0
- {valuebridge_tdfloat-1.0.0 → valuebridge_tdfloat-1.1.0}/valuebridge/tdfloat/constants.py +0 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
include README.md
|
|
2
|
+
include LICENSE
|
|
3
|
+
include pyproject.toml
|
|
4
|
+
|
|
5
|
+
recursive-include valuebridge *.py
|
|
6
|
+
|
|
7
|
+
# Keep the sdist lean: exclude Rust crate, Coq proofs, docs, tests,
|
|
8
|
+
# build artifacts, and lockfiles. These live in the git repository.
|
|
9
|
+
prune tdfloat_core
|
|
10
|
+
prune proofs
|
|
11
|
+
prune docs
|
|
12
|
+
prune tests
|
|
13
|
+
prune dist
|
|
14
|
+
prune build
|
|
15
|
+
prune .venv
|
|
16
|
+
prune valuebridge_tdfloat.egg-info
|
|
17
|
+
exclude uv.lock
|
|
18
|
+
exclude .gitignore
|
|
19
|
+
global-exclude __pycache__
|
|
20
|
+
global-exclude *.pyc
|
|
21
|
+
global-exclude *.pyo
|
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: valuebridge-tdfloat
|
|
3
|
+
Version: 1.1.0
|
|
4
|
+
Summary: Triadic Dot Float — exact rational arithmetic as a single Python integer
|
|
5
|
+
Author-email: Tushar Dadlani <tushar@valuebridge.ai>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/valuebridge-ai/tdfloat
|
|
8
|
+
Project-URL: Source, https://github.com/valuebridge-ai/tdfloat
|
|
9
|
+
Project-URL: Bug Tracker, https://github.com/valuebridge-ai/tdfloat/issues
|
|
10
|
+
Keywords: arithmetic,rational,exact,floating-point,mathematics,encoding,number-theory
|
|
11
|
+
Classifier: Development Status :: 5 - Production/Stable
|
|
12
|
+
Classifier: Intended Audience :: Science/Research
|
|
13
|
+
Classifier: Intended Audience :: Education
|
|
14
|
+
Classifier: Intended Audience :: Developers
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
22
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
23
|
+
Classifier: Topic :: Scientific/Engineering :: Mathematics
|
|
24
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
25
|
+
Requires-Python: >=3.8
|
|
26
|
+
Description-Content-Type: text/markdown
|
|
27
|
+
License-File: LICENSE
|
|
28
|
+
Dynamic: license-file
|
|
29
|
+
|
|
30
|
+
# valuebridge-tdfloat
|
|
31
|
+
|
|
32
|
+
**Triadic Dot Float (TDFloat)** — exact rational arithmetic as a single
|
|
33
|
+
Python integer, with a Rust backend and Coq-verified foundations.
|
|
34
|
+
|
|
35
|
+
```python
|
|
36
|
+
from valuebridge.tdfloat import td, frac
|
|
37
|
+
|
|
38
|
+
td('0.1') + td('0.2') == td('0.3') # True — always
|
|
39
|
+
(a + b) + c == a + (b + c) # True — for any a, b, c
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
---
|
|
43
|
+
|
|
44
|
+
## Why This Matters for Explainable AI
|
|
45
|
+
|
|
46
|
+
Modern AI systems — language models, recommendation engines, fairness
|
|
47
|
+
audits, financial models — all perform millions of arithmetic operations.
|
|
48
|
+
Every one of those operations runs on IEEE 754 floating-point, a format
|
|
49
|
+
designed in 1985 for numerical simulation, not for systems that need to
|
|
50
|
+
be explained, audited, or trusted.
|
|
51
|
+
|
|
52
|
+
**TDFloat replaces floating-point approximation with provably exact
|
|
53
|
+
rational arithmetic.** The consequences for explainable AI are direct.
|
|
54
|
+
|
|
55
|
+
### 1. Computations Are Reproducible — Exactly
|
|
56
|
+
|
|
57
|
+
IEEE 754 arithmetic is non-deterministic across platforms, compilers, and
|
|
58
|
+
thread orderings. TDFloat arithmetic is deterministic by construction.
|
|
59
|
+
The same inputs always produce the same output, encoded as the same
|
|
60
|
+
integer. AI outputs become **auditable**: you can replay any computation
|
|
61
|
+
and verify it step by step.
|
|
62
|
+
|
|
63
|
+
### 2. Associativity is a Theorem, Not a Hope
|
|
64
|
+
|
|
65
|
+
In IEEE 754:
|
|
66
|
+
|
|
67
|
+
```
|
|
68
|
+
(a + b) + c ≠ a + (b + c) for many values
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
Simply reordering additions in a neural network — which optimising
|
|
72
|
+
compilers do routinely — can change the model's output.
|
|
73
|
+
|
|
74
|
+
TDFloat's associativity is **formally proved in Coq**
|
|
75
|
+
(`proofs/tdfloat_ieee_resolution.v`).
|
|
76
|
+
|
|
77
|
+
### 3. Every Number Has an Interpretable Identity
|
|
78
|
+
|
|
79
|
+
In IEEE 754, `3.141592653589793` is just a 64-bit pattern. In TDFloat:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
x = td('3.14')
|
|
83
|
+
x.as_fraction() # (157, 50) — exact numerator and denominator
|
|
84
|
+
x.info_bit # 1 — half-step axis (has a dot)
|
|
85
|
+
x.dot_pos # 2 — two decimal places
|
|
86
|
+
```
|
|
87
|
+
|
|
88
|
+
Every intermediate result is fully inspectable as an exact rational.
|
|
89
|
+
|
|
90
|
+
### 4. Fairness Audits Can Be Verified
|
|
91
|
+
|
|
92
|
+
If two computations produce the same rational value, their encodings are
|
|
93
|
+
equal. If different, provably different. There is no grey zone of "close
|
|
94
|
+
enough." Fairness claims can be verified arithmetically, not just
|
|
95
|
+
statistically.
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Quick Start
|
|
100
|
+
|
|
101
|
+
```python
|
|
102
|
+
from valuebridge.tdfloat import td, frac, TDFloat, PI, E, PHI
|
|
103
|
+
from valuebridge.tdfloat.math import sqrt, circle_area, cosine_similarity
|
|
104
|
+
|
|
105
|
+
# Exact arithmetic
|
|
106
|
+
assert td('0.1') + td('0.2') == td('0.3')
|
|
107
|
+
|
|
108
|
+
# Exact (legacy-rational) constants
|
|
109
|
+
print(PI) # 22/7
|
|
110
|
+
print(E) # 19/7
|
|
111
|
+
print(circle_area(td(7))) # 154 — exact integer
|
|
112
|
+
|
|
113
|
+
# Exact vector similarity
|
|
114
|
+
u = [td(3), td(4)]
|
|
115
|
+
v = [td(4), td(3)]
|
|
116
|
+
print(cosine_similarity(u, v)) # 24/25 — exact
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
### IEEE 754 / MPFR-accurate constants
|
|
120
|
+
|
|
121
|
+
For numerically-accurate values of irrational constants (π, e, √2, ln 2,
|
|
122
|
+
…), use the parallel `ieee754` namespace:
|
|
123
|
+
|
|
124
|
+
```python
|
|
125
|
+
from valuebridge.tdfloat import ieee754
|
|
126
|
+
|
|
127
|
+
ieee754.PI() # π at 200 bits (default)
|
|
128
|
+
ieee754.PI(bits=53) # π at IEEE 754 double precision
|
|
129
|
+
ieee754.E(bits=128)
|
|
130
|
+
ieee754.SQRT2(bits=256)
|
|
131
|
+
ieee754.LN2(bits=64)
|
|
132
|
+
ieee754.EULER_GAMMA()
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
Each constant is computed by GNU MPFR (via `gmpy2`) at the requested
|
|
136
|
+
precision and returned as a `TDFloat` rational approximation. The legacy
|
|
137
|
+
`PI = 22/7` stays unchanged for backward compat.
|
|
138
|
+
|
|
139
|
+
`gmpy2` is a development-group dependency; install with
|
|
140
|
+
`uv sync --group dev` to enable.
|
|
141
|
+
|
|
142
|
+
---
|
|
143
|
+
|
|
144
|
+
## Installation
|
|
145
|
+
|
|
146
|
+
```bash
|
|
147
|
+
# With uv (recommended)
|
|
148
|
+
uv add valuebridge-tdfloat
|
|
149
|
+
|
|
150
|
+
# With pip
|
|
151
|
+
pip install valuebridge-tdfloat
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
For the Rust backend (optional; accelerates large-operand arithmetic):
|
|
155
|
+
|
|
156
|
+
```bash
|
|
157
|
+
cd tdfloat_core
|
|
158
|
+
maturin develop --release
|
|
159
|
+
```
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Architecture
|
|
164
|
+
|
|
165
|
+
```
|
|
166
|
+
Coq specification proofs/*.v
|
|
167
|
+
│ HelixBit → BigInt → helix-closed arithmetic
|
|
168
|
+
▼ (formal refinement)
|
|
169
|
+
Rust backend tdfloat_core/
|
|
170
|
+
│ TDBigInt { sign, Arc<[u64]> limbs } + 5 ops
|
|
171
|
+
▼ (PyO3 boundary)
|
|
172
|
+
Python surface valuebridge/tdfloat/
|
|
173
|
+
│ TDFloat exact rational p/q, dot-axis encoding
|
|
174
|
+
│ math funcs, vector ops
|
|
175
|
+
▼
|
|
176
|
+
IEEE 754 reference layer valuebridge/tdfloat/ieee754/
|
|
177
|
+
MPFR-backed irrational constants at configurable precision
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Each layer has one job. Each arrow is a formal refinement that preserves
|
|
181
|
+
the observable behavior of the layer above it.
|
|
182
|
+
|
|
183
|
+
See:
|
|
184
|
+
|
|
185
|
+
- [`docs/mathematics.md`](./docs/mathematics.md) — the triadic geometry,
|
|
186
|
+
half-step encoding, NAND double helix, how all five arithmetic
|
|
187
|
+
operators are grounded in one bit-level construction.
|
|
188
|
+
- [`docs/implementation.md`](./docs/implementation.md) — how the Python
|
|
189
|
+
surface, Rust backend, and Coq proofs fit together; testing,
|
|
190
|
+
benchmarks, development loop.
|
|
191
|
+
|
|
192
|
+
---
|
|
193
|
+
|
|
194
|
+
## Formal Verification
|
|
195
|
+
|
|
196
|
+
The mathematical foundations are proved in Coq. Every `.v` file has a
|
|
197
|
+
companion `.md` explaining it in prose.
|
|
198
|
+
|
|
199
|
+
| Proof file | What it proves |
|
|
200
|
+
| ---------------------------------- | -------------- |
|
|
201
|
+
| `encoding_any_symbol.v` | The abstract half-step encoding is injective and reversible |
|
|
202
|
+
| `tdfloat_dot_encoding.v` | The `"."` IS the info-bit; integer and fractional encodings never collide |
|
|
203
|
+
| `tdfloat_ieee_resolution.v` | TDFloat addition is associative; IEEE 754 addition provably is not |
|
|
204
|
+
| `NANDDoubleHelix.v` | A bit is two perpendicular strands; all gates derive from NAND |
|
|
205
|
+
| `BitwiseArbitraryInt.v` | Complete BigInt library (or/and/xor/not/shl/shr/add/mul/compare) with 10 invariants |
|
|
206
|
+
| `HelixArithClosed.v` | Subtraction (`a + ¬a + 1 ≡ 0 mod 2ⁿ`), division (shift-and-subtract with invariant), modulo |
|
|
207
|
+
|
|
208
|
+
These are machine-checked proofs, not documentation claims. The Rust
|
|
209
|
+
backend is a refinement of the Coq spec — its `get_bit(k)` returns the
|
|
210
|
+
same value as the Coq `get_bit`, so every theorem lifts observationally.
|
|
211
|
+
|
|
212
|
+
Run with:
|
|
213
|
+
|
|
214
|
+
```bash
|
|
215
|
+
coqc proofs/*.v
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Development
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
git clone https://github.com/valuebridge-ai/tdfloat
|
|
224
|
+
cd tdfloat
|
|
225
|
+
|
|
226
|
+
# Python layer
|
|
227
|
+
uv sync --group dev
|
|
228
|
+
uv run pytest tests/ -q
|
|
229
|
+
# 268 passed, 15 xfailed, 4 xpassed
|
|
230
|
+
|
|
231
|
+
# Rust backend
|
|
232
|
+
cd tdfloat_core
|
|
233
|
+
VIRTUAL_ENV=../.venv maturin develop --release
|
|
234
|
+
cargo test --release --lib
|
|
235
|
+
# 27 passed; 0 failed
|
|
236
|
+
|
|
237
|
+
# Benchmark Rust vs Python
|
|
238
|
+
uv run python tests/bench_rust_vs_python.py
|
|
239
|
+
|
|
240
|
+
# Compile proofs
|
|
241
|
+
cd ../proofs
|
|
242
|
+
coqc *.v
|
|
243
|
+
```
|
|
244
|
+
|
|
245
|
+
---
|
|
246
|
+
|
|
247
|
+
## Project Layout
|
|
248
|
+
|
|
249
|
+
```
|
|
250
|
+
valuebridge-tdfloat/
|
|
251
|
+
├── valuebridge/tdfloat/ Python surface (TDFloat, math, ieee754)
|
|
252
|
+
├── tdfloat_core/ Rust backend (PyO3 + maturin)
|
|
253
|
+
├── proofs/ Coq specifications + .md explanations
|
|
254
|
+
├── tests/ pytest (268 tests) + benchmark
|
|
255
|
+
├── docs/ mathematics.md, implementation.md
|
|
256
|
+
└── pyproject.toml
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
---
|
|
260
|
+
|
|
261
|
+
## License
|
|
262
|
+
|
|
263
|
+
MIT — Copyright 2026 Tushar Dadlani / Valuebridge AI
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
# valuebridge-tdfloat
|
|
2
|
+
|
|
3
|
+
**Triadic Dot Float (TDFloat)** — exact rational arithmetic as a single
|
|
4
|
+
Python integer, with a Rust backend and Coq-verified foundations.
|
|
5
|
+
|
|
6
|
+
```python
|
|
7
|
+
from valuebridge.tdfloat import td, frac
|
|
8
|
+
|
|
9
|
+
td('0.1') + td('0.2') == td('0.3') # True — always
|
|
10
|
+
(a + b) + c == a + (b + c) # True — for any a, b, c
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
---
|
|
14
|
+
|
|
15
|
+
## Why This Matters for Explainable AI
|
|
16
|
+
|
|
17
|
+
Modern AI systems — language models, recommendation engines, fairness
|
|
18
|
+
audits, financial models — all perform millions of arithmetic operations.
|
|
19
|
+
Every one of those operations runs on IEEE 754 floating-point, a format
|
|
20
|
+
designed in 1985 for numerical simulation, not for systems that need to
|
|
21
|
+
be explained, audited, or trusted.
|
|
22
|
+
|
|
23
|
+
**TDFloat replaces floating-point approximation with provably exact
|
|
24
|
+
rational arithmetic.** The consequences for explainable AI are direct.
|
|
25
|
+
|
|
26
|
+
### 1. Computations Are Reproducible — Exactly
|
|
27
|
+
|
|
28
|
+
IEEE 754 arithmetic is non-deterministic across platforms, compilers, and
|
|
29
|
+
thread orderings. TDFloat arithmetic is deterministic by construction.
|
|
30
|
+
The same inputs always produce the same output, encoded as the same
|
|
31
|
+
integer. AI outputs become **auditable**: you can replay any computation
|
|
32
|
+
and verify it step by step.
|
|
33
|
+
|
|
34
|
+
### 2. Associativity is a Theorem, Not a Hope
|
|
35
|
+
|
|
36
|
+
In IEEE 754:
|
|
37
|
+
|
|
38
|
+
```
|
|
39
|
+
(a + b) + c ≠ a + (b + c) for many values
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Simply reordering additions in a neural network — which optimising
|
|
43
|
+
compilers do routinely — can change the model's output.
|
|
44
|
+
|
|
45
|
+
TDFloat's associativity is **formally proved in Coq**
|
|
46
|
+
(`proofs/tdfloat_ieee_resolution.v`).
|
|
47
|
+
|
|
48
|
+
### 3. Every Number Has an Interpretable Identity
|
|
49
|
+
|
|
50
|
+
In IEEE 754, `3.141592653589793` is just a 64-bit pattern. In TDFloat:
|
|
51
|
+
|
|
52
|
+
```python
|
|
53
|
+
x = td('3.14')
|
|
54
|
+
x.as_fraction() # (157, 50) — exact numerator and denominator
|
|
55
|
+
x.info_bit # 1 — half-step axis (has a dot)
|
|
56
|
+
x.dot_pos # 2 — two decimal places
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
Every intermediate result is fully inspectable as an exact rational.
|
|
60
|
+
|
|
61
|
+
### 4. Fairness Audits Can Be Verified
|
|
62
|
+
|
|
63
|
+
If two computations produce the same rational value, their encodings are
|
|
64
|
+
equal. If different, provably different. There is no grey zone of "close
|
|
65
|
+
enough." Fairness claims can be verified arithmetically, not just
|
|
66
|
+
statistically.
|
|
67
|
+
|
|
68
|
+
---
|
|
69
|
+
|
|
70
|
+
## Quick Start
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
from valuebridge.tdfloat import td, frac, TDFloat, PI, E, PHI
|
|
74
|
+
from valuebridge.tdfloat.math import sqrt, circle_area, cosine_similarity
|
|
75
|
+
|
|
76
|
+
# Exact arithmetic
|
|
77
|
+
assert td('0.1') + td('0.2') == td('0.3')
|
|
78
|
+
|
|
79
|
+
# Exact (legacy-rational) constants
|
|
80
|
+
print(PI) # 22/7
|
|
81
|
+
print(E) # 19/7
|
|
82
|
+
print(circle_area(td(7))) # 154 — exact integer
|
|
83
|
+
|
|
84
|
+
# Exact vector similarity
|
|
85
|
+
u = [td(3), td(4)]
|
|
86
|
+
v = [td(4), td(3)]
|
|
87
|
+
print(cosine_similarity(u, v)) # 24/25 — exact
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
### IEEE 754 / MPFR-accurate constants
|
|
91
|
+
|
|
92
|
+
For numerically-accurate values of irrational constants (π, e, √2, ln 2,
|
|
93
|
+
…), use the parallel `ieee754` namespace:
|
|
94
|
+
|
|
95
|
+
```python
|
|
96
|
+
from valuebridge.tdfloat import ieee754
|
|
97
|
+
|
|
98
|
+
ieee754.PI() # π at 200 bits (default)
|
|
99
|
+
ieee754.PI(bits=53) # π at IEEE 754 double precision
|
|
100
|
+
ieee754.E(bits=128)
|
|
101
|
+
ieee754.SQRT2(bits=256)
|
|
102
|
+
ieee754.LN2(bits=64)
|
|
103
|
+
ieee754.EULER_GAMMA()
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
Each constant is computed by GNU MPFR (via `gmpy2`) at the requested
|
|
107
|
+
precision and returned as a `TDFloat` rational approximation. The legacy
|
|
108
|
+
`PI = 22/7` stays unchanged for backward compat.
|
|
109
|
+
|
|
110
|
+
`gmpy2` is a development-group dependency; install with
|
|
111
|
+
`uv sync --group dev` to enable.
|
|
112
|
+
|
|
113
|
+
---
|
|
114
|
+
|
|
115
|
+
## Installation
|
|
116
|
+
|
|
117
|
+
```bash
|
|
118
|
+
# With uv (recommended)
|
|
119
|
+
uv add valuebridge-tdfloat
|
|
120
|
+
|
|
121
|
+
# With pip
|
|
122
|
+
pip install valuebridge-tdfloat
|
|
123
|
+
```
|
|
124
|
+
|
|
125
|
+
For the Rust backend (optional; accelerates large-operand arithmetic):
|
|
126
|
+
|
|
127
|
+
```bash
|
|
128
|
+
cd tdfloat_core
|
|
129
|
+
maturin develop --release
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
---
|
|
133
|
+
|
|
134
|
+
## Architecture
|
|
135
|
+
|
|
136
|
+
```
|
|
137
|
+
Coq specification proofs/*.v
|
|
138
|
+
│ HelixBit → BigInt → helix-closed arithmetic
|
|
139
|
+
▼ (formal refinement)
|
|
140
|
+
Rust backend tdfloat_core/
|
|
141
|
+
│ TDBigInt { sign, Arc<[u64]> limbs } + 5 ops
|
|
142
|
+
▼ (PyO3 boundary)
|
|
143
|
+
Python surface valuebridge/tdfloat/
|
|
144
|
+
│ TDFloat exact rational p/q, dot-axis encoding
|
|
145
|
+
│ math funcs, vector ops
|
|
146
|
+
▼
|
|
147
|
+
IEEE 754 reference layer valuebridge/tdfloat/ieee754/
|
|
148
|
+
MPFR-backed irrational constants at configurable precision
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
Each layer has one job. Each arrow is a formal refinement that preserves
|
|
152
|
+
the observable behavior of the layer above it.
|
|
153
|
+
|
|
154
|
+
See:
|
|
155
|
+
|
|
156
|
+
- [`docs/mathematics.md`](./docs/mathematics.md) — the triadic geometry,
|
|
157
|
+
half-step encoding, NAND double helix, how all five arithmetic
|
|
158
|
+
operators are grounded in one bit-level construction.
|
|
159
|
+
- [`docs/implementation.md`](./docs/implementation.md) — how the Python
|
|
160
|
+
surface, Rust backend, and Coq proofs fit together; testing,
|
|
161
|
+
benchmarks, development loop.
|
|
162
|
+
|
|
163
|
+
---
|
|
164
|
+
|
|
165
|
+
## Formal Verification
|
|
166
|
+
|
|
167
|
+
The mathematical foundations are proved in Coq. Every `.v` file has a
|
|
168
|
+
companion `.md` explaining it in prose.
|
|
169
|
+
|
|
170
|
+
| Proof file | What it proves |
|
|
171
|
+
| ---------------------------------- | -------------- |
|
|
172
|
+
| `encoding_any_symbol.v` | The abstract half-step encoding is injective and reversible |
|
|
173
|
+
| `tdfloat_dot_encoding.v` | The `"."` IS the info-bit; integer and fractional encodings never collide |
|
|
174
|
+
| `tdfloat_ieee_resolution.v` | TDFloat addition is associative; IEEE 754 addition provably is not |
|
|
175
|
+
| `NANDDoubleHelix.v` | A bit is two perpendicular strands; all gates derive from NAND |
|
|
176
|
+
| `BitwiseArbitraryInt.v` | Complete BigInt library (or/and/xor/not/shl/shr/add/mul/compare) with 10 invariants |
|
|
177
|
+
| `HelixArithClosed.v` | Subtraction (`a + ¬a + 1 ≡ 0 mod 2ⁿ`), division (shift-and-subtract with invariant), modulo |
|
|
178
|
+
|
|
179
|
+
These are machine-checked proofs, not documentation claims. The Rust
|
|
180
|
+
backend is a refinement of the Coq spec — its `get_bit(k)` returns the
|
|
181
|
+
same value as the Coq `get_bit`, so every theorem lifts observationally.
|
|
182
|
+
|
|
183
|
+
Run with:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
coqc proofs/*.v
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
---
|
|
190
|
+
|
|
191
|
+
## Development
|
|
192
|
+
|
|
193
|
+
```bash
|
|
194
|
+
git clone https://github.com/valuebridge-ai/tdfloat
|
|
195
|
+
cd tdfloat
|
|
196
|
+
|
|
197
|
+
# Python layer
|
|
198
|
+
uv sync --group dev
|
|
199
|
+
uv run pytest tests/ -q
|
|
200
|
+
# 268 passed, 15 xfailed, 4 xpassed
|
|
201
|
+
|
|
202
|
+
# Rust backend
|
|
203
|
+
cd tdfloat_core
|
|
204
|
+
VIRTUAL_ENV=../.venv maturin develop --release
|
|
205
|
+
cargo test --release --lib
|
|
206
|
+
# 27 passed; 0 failed
|
|
207
|
+
|
|
208
|
+
# Benchmark Rust vs Python
|
|
209
|
+
uv run python tests/bench_rust_vs_python.py
|
|
210
|
+
|
|
211
|
+
# Compile proofs
|
|
212
|
+
cd ../proofs
|
|
213
|
+
coqc *.v
|
|
214
|
+
```
|
|
215
|
+
|
|
216
|
+
---
|
|
217
|
+
|
|
218
|
+
## Project Layout
|
|
219
|
+
|
|
220
|
+
```
|
|
221
|
+
valuebridge-tdfloat/
|
|
222
|
+
├── valuebridge/tdfloat/ Python surface (TDFloat, math, ieee754)
|
|
223
|
+
├── tdfloat_core/ Rust backend (PyO3 + maturin)
|
|
224
|
+
├── proofs/ Coq specifications + .md explanations
|
|
225
|
+
├── tests/ pytest (268 tests) + benchmark
|
|
226
|
+
├── docs/ mathematics.md, implementation.md
|
|
227
|
+
└── pyproject.toml
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
---
|
|
231
|
+
|
|
232
|
+
## License
|
|
233
|
+
|
|
234
|
+
MIT — Copyright 2026 Tushar Dadlani / Valuebridge AI
|
|
@@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"
|
|
|
4
4
|
|
|
5
5
|
[project]
|
|
6
6
|
name = "valuebridge-tdfloat"
|
|
7
|
-
version = "1.
|
|
7
|
+
version = "1.1.0"
|
|
8
8
|
description = "Triadic Dot Float — exact rational arithmetic as a single Python integer"
|
|
9
9
|
readme = "README.md"
|
|
10
10
|
license = "MIT"
|
|
@@ -44,4 +44,8 @@ where = ["."]
|
|
|
44
44
|
include = ["valuebridge*"]
|
|
45
45
|
|
|
46
46
|
[dependency-groups]
|
|
47
|
-
dev = ["pytest>=8"]
|
|
47
|
+
dev = ["pytest>=8", "gmpy2>=2.1"]
|
|
48
|
+
|
|
49
|
+
[tool.pytest.ini_options]
|
|
50
|
+
testpaths = ["tests"]
|
|
51
|
+
|
|
@@ -39,6 +39,7 @@ All constants are exact rationals:
|
|
|
39
39
|
from .tdfloat import TDFloat, td, frac, divmod_td
|
|
40
40
|
from . import math
|
|
41
41
|
from . import constants
|
|
42
|
+
from . import ieee754
|
|
42
43
|
|
|
43
44
|
# Expose constants at package level
|
|
44
45
|
PI = TDFloat.PI
|
|
@@ -60,5 +61,5 @@ __all__ = [
|
|
|
60
61
|
'TDFloat', 'td', 'frac', 'divmod_td',
|
|
61
62
|
'PI', 'TAU', 'E', 'PHI', 'SQRT2', 'SQRT3', 'HALF',
|
|
62
63
|
'ZERO', 'ONE', 'TWO', 'NAN', 'INF',
|
|
63
|
-
'math', 'constants',
|
|
64
|
+
'math', 'constants', 'ieee754',
|
|
64
65
|
]
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"""
|
|
2
|
+
valuebridge.tdfloat.ieee754
|
|
3
|
+
===========================
|
|
4
|
+
MPFR-computed reference constants in IEEE 754 / MPFR semantics.
|
|
5
|
+
|
|
6
|
+
This submodule is parallel to the legacy ``valuebridge.tdfloat`` surface:
|
|
7
|
+
the legacy constants stay as exact rational definitions (``PI = 22/7`` etc.)
|
|
8
|
+
for backward compatibility, while this namespace exposes the mathematically
|
|
9
|
+
correct irrational values at user-selectable precision, computed via GNU MPFR
|
|
10
|
+
through the ``gmpy2`` binding.
|
|
11
|
+
|
|
12
|
+
Usage
|
|
13
|
+
-----
|
|
14
|
+
from valuebridge.tdfloat import ieee754
|
|
15
|
+
|
|
16
|
+
pi200 = ieee754.PI() # default 200 bits
|
|
17
|
+
pi53 = ieee754.PI(bits=53) # IEEE 754 double precision
|
|
18
|
+
area = ieee754.PI * 5 * 5 # usable as a TDFloat-like value
|
|
19
|
+
|
|
20
|
+
Dependencies
|
|
21
|
+
------------
|
|
22
|
+
Requires ``gmpy2`` (a Python binding for GNU MPFR/GMP). Install as a dev
|
|
23
|
+
dependency; the base library remains pure-Python.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
from ._precision import DEFAULT_BITS
|
|
27
|
+
from ._mpfr_bridge import mpfr_to_tdfloat
|
|
28
|
+
from .constants import (
|
|
29
|
+
PI, TAU, E, LN2, LN10, LOG2E, LOG10E,
|
|
30
|
+
SQRT2, SQRT3, PHI, EULER_GAMMA, CATALAN,
|
|
31
|
+
)
|
|
32
|
+
|
|
33
|
+
__all__ = [
|
|
34
|
+
'PI', 'TAU', 'E', 'LN2', 'LN10', 'LOG2E', 'LOG10E',
|
|
35
|
+
'SQRT2', 'SQRT3', 'PHI', 'EULER_GAMMA', 'CATALAN',
|
|
36
|
+
'DEFAULT_BITS', 'mpfr_to_tdfloat',
|
|
37
|
+
]
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"""MPFR mpfr <-> TDFloat conversion.
|
|
2
|
+
|
|
3
|
+
Uses gmpy2.f2q to extract the exact dyadic rational of an MPFR value, then
|
|
4
|
+
builds a TDFloat from (numerator, denominator). gmpy2 is imported lazily so
|
|
5
|
+
the base library stays pure-Python if gmpy2 is not installed.
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from ..tdfloat import TDFloat
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def _require_gmpy2():
|
|
14
|
+
try:
|
|
15
|
+
import gmpy2
|
|
16
|
+
except ImportError as e:
|
|
17
|
+
raise ImportError(
|
|
18
|
+
"valuebridge.tdfloat.ieee754 requires gmpy2. "
|
|
19
|
+
"Install with: pip install gmpy2"
|
|
20
|
+
) from e
|
|
21
|
+
return gmpy2
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def mpfr_to_tdfloat(m) -> TDFloat:
|
|
25
|
+
"""Convert an MPFR value to an exact TDFloat (dyadic rational)."""
|
|
26
|
+
gmpy2 = _require_gmpy2()
|
|
27
|
+
if m.is_nan():
|
|
28
|
+
return TDFloat.NAN
|
|
29
|
+
if m.is_infinite():
|
|
30
|
+
return TDFloat.INF if m > 0 else TDFloat.NINF
|
|
31
|
+
q = gmpy2.f2q(m, 0)
|
|
32
|
+
p, d = q.numerator, q.denominator
|
|
33
|
+
return TDFloat.from_frac(int(p), int(d))
|