The most paranoid addition ever created
Project description
๐ฅ NUCLEAR ADD v1.0.0
The most paranoid addition ever created.
A simple addition? No. A militarized numerical computation engine.
from nuclear_add import add
# Normal addition... but better
add(0.1, 0.2) # It works!
# The classic float problem
0.1 + 0.2 == 0.3 # False (native Python)
add(0.1, 0.2, precision="decimal") == Decimal("0.3") # True โ
# Overflow? No more silent surprises
1e308 + 1e308 # inf (native Python, silent)
add(1e308, 1e308) # OverflowError! (Nuclear Add)
๐ฏ Why This Project Exists
Because an addition can:
- Lose precision (0.1 + 0.2 โ 0.3)
- Overflow silently (1e308 + 1e308 โ inf)
- Propagate NaN without warning
- Accumulate errors in long sums
- Hide bugs in critical scientific code
This module solves all these problems.
๐ Features
1. Precision Modes
add(0.1, 0.2, precision="decimal") # Decimal('0.3')
add(0.1, 0.2, precision="fraction") # Fraction(3, 10)
add(0.1, 0.2, precision="interval") # Interval with error bounds
2. Error Handling
# Overflow
add(1e308, 1e308, overflow="raise") # OverflowError
add(1e308, 1e308, overflow="inf") # inf
add(1e308, 1e308, overflow="saturate") # 1.79e308
# NaN
add(float('nan'), 1, nan="raise") # ArithmeticError
add(float('nan'), 1, nan="propagate") # nan
add(float('nan'), 1, nan="replace") # 0.0
3. Interval Arithmetic
from nuclear_add.types import Interval
a = Interval.from_value(0.1)
b = Interval.from_value(0.2)
c = a + b
print(0.3 in c) # True - true value is guaranteed in interval
4. Automatic Differentiation
from nuclear_add.types import DualNumber
from nuclear_add import gradient
# Gradient computation
def f(x):
return x * x * x # f(x) = xยณ
gradient(f, 2.0) # 12.0 (= 3ร2ยฒ = f'(2))
5. Kahan Summation
from nuclear_add import sum_safe
# Sum of 10 million numbers
values = [1.0] * 10_000_000
sum(values) # Accumulated rounding error
sum_safe(values) # Maximum precision with Kahan
6. Lazy Evaluation
from nuclear_add.types import LazyExpr
x = LazyExpr.var("x", 3.0)
y = LazyExpr.var("y", 4.0)
z = (x * x + y * y).sqrt() # Not computed yet!
z.eval() # 5.0 (computed now)
z.grad("x") # Symbolic gradient
z.to_graph() # Computation graph DOT
7. Error Tracing
from nuclear_add.tracing import NumericTracer
tracer = NumericTracer()
# ... computations ...
tracer.get_summary() # Summary of all anomalies
tracer.to_json() # Export for analysis
8. Vectorization
add([1, 2, 3], [4, 5, 6]) # [5, 7, 9]
add([1, 2, 3], 10) # [11, 12, 13] (broadcasting)
9. Multiple Backends
from nuclear_add.backends import get_backend
# Pure Python (portable)
get_backend("python")
# NumPy SIMD (vectorized)
get_backend("numpy")
# CuPy GPU (CUDA)
get_backend("cupy")
# Numba JIT (compiled)
get_backend("numba")
# Decimal (arbitrary precision)
get_backend("decimal", precision=100)
10. Unit Support (Pint)
from pint import UnitRegistry
ureg = UnitRegistry()
add(10 * ureg.meter, 5 * ureg.meter) # 15 meter
add(10 * ureg.meter, 5 * ureg.second) # DimensionalityError!
๐ฆ Installation
Local Development Installation (for use in other projects)
To use this module in other projects without downloading/cloning:
# Navigate to the project directory
cd C:\Users\jessy\Documents\dev\nuclear_add\nuclear_add
# Install in editable mode (recommended)
uv pip install -e .
# Or with development dependencies
uv pip install -e ".[dev]"
Now you can use it in any Python project:
from nuclear_add import add
result = add(2, 3) # 5
From Another Project
If you're in another project and want to use nuclear_add:
# Install from local path
uv pip install -e C:\Users\jessy\Documents\dev\nuclear_add\nuclear_add
# Or add to PYTHONPATH
# Windows PowerShell:
$env:PYTHONPATH = "C:\Users\jessy\Documents\dev\nuclear_add\nuclear_add\src;$env:PYTHONPATH"
Development Setup
# Clone the repository
git clone https://github.com/example/nuclear-add.git
cd nuclear-add
# Install dependencies (base)
uv sync
# Install with development dependencies
uv sync --extra dev
# Run the demo
uv run python -m nuclear_add.demo
# Run tests
uv run pytest
Using pip
# Base installation
pip install nuclear-add
# With NumPy
pip install nuclear-add[numpy]
# With GPU support
pip install nuclear-add[gpu]
# With JIT compilation
pip install nuclear-add[jit]
# With units
pip install nuclear-add[units]
# With symbolic fallback
pip install nuclear-add[symbolic]
# Install everything
pip install nuclear-add[all]
๐ฎ Configuration Presets
from nuclear_add.core import NuclearConfig, NuclearEngine
# Strict mode (default)
config = NuclearConfig.strict()
# Fast mode (fewer checks)
config = NuclearConfig.fast()
# Paranoid mode (ALL checks)
config = NuclearConfig.paranoid()
# Scientific mode (high precision)
config = NuclearConfig.scientific(precision=100)
# Use with custom engine
engine = NuclearEngine(config)
engine.add(a, b)
๐ฌ Use Cases
Finance / Trading
# No surprises with amounts
add(Decimal("100.50"), Decimal("0.25"), precision="decimal")
Scientific Computing
# Precise sums of large series
sum_safe(measurements, precision="kahan")
Physics Simulation
# Uncertainty propagation
pos = Interval.from_value(initial_pos, ulp_error=1)
for dt in time_steps:
pos = add(pos, velocity * dt)
print(f"Final position: {pos}, uncertainty: {pos.width}")
Machine Learning
# Automatic gradients
loss = compute_loss(DualNumber.variable(weight))
gradient = loss.dual
๐ Comparison
| Operation | Native Python | Nuclear Add |
|---|---|---|
0.1 + 0.2 |
0.30000000000000004 |
Decimal('0.3') |
1e308 + 1e308 |
inf (silent) |
OverflowError |
nan + 1 |
nan (propagates) |
ArithmeticError |
| Sum of 10M values | Accumulated errors | Kahan compensated |
| Gradient of f(x) | Finite differences | Exact autodiff |
๐งช Testing
# Run tests with uv
uv run pytest tests/ -v --cov=src/nuclear_add
# Or with pip
pytest tests/ -v --cov=nuclear_add
๐ Documentation
Documentation complรจte disponible dans le dossier docs/ :
- Index - Point d'entrรฉe de la documentation
- Architecture - Vue d'ensemble de l'architecture
- API Reference - Documentation complรจte de l'API
- Guide des mรฉthodes - Exemples pratiques
- Dรฉcisions de conception - Choix techniques
- Diagrammes - Schรฉmas et diagrammes visuels (Mermaid)
- Guide de contribution - Comment contribuer
๐ ๏ธ Development
Setup
# Install uv (if not already installed)
curl -LsSf https://astral.sh/uv/install.sh | sh
# Clone repository
git clone https://github.com/example/nuclear-add.git
cd nuclear-add
# Install dependencies
uv sync --extra dev
# Run tests
uv run pytest
# Run linters
uv run ruff check src/ tests/
uv run black --check src/ tests/
uv run mypy src/nuclear_add
Project Structure
nuclear_add/
โโโ src/
โ โโโ nuclear_add/
โ โโโ __init__.py
โ โโโ core.py # Core engine and configuration
โ โโโ types.py # Advanced types (Interval, DualNumber, etc.)
โ โโโ backends.py # Computation backends
โ โโโ tracing.py # Error tracing system
โ โโโ demo.py # Demonstration script
โโโ tests/
โ โโโ test_core.py
โ โโโ test_types.py
โ โโโ test_backends.py
โ โโโ test_tracing.py
โโโ docs/
โ โโโ index.md # Documentation index
โ โโโ architecture.md # System architecture
โ โโโ api_reference.md # Complete API reference
โ โโโ methods_guide.md # Practical usage guide
โ โโโ design_decisions.md # Design decisions
โ โโโ diagrams.md # Visual diagrams
โ โโโ contributing.md # Contributing guide
โโโ .github/
โ โโโ workflows/
โ โโโ ci.yml # CI/CD configuration
โโโ pyproject.toml # Project configuration
โโโ README.md # This file
โโโ CHANGELOG.md # Changelog
๐ License
MIT - Use this code for whatever you want.
๐ค FAQ
Q: Is this really useful?
A: For 99.9% of cases, use +. For the critical 0.1% (finance, science, simulations), yes.
Q: Isn't this overkill? A: That's literally the point of the project.
Q: Performance?
A: fast mode = quasi-native. paranoid mode = 10-100x slower but bulletproof.
Q: Why does add(1e308, 1e308) raise an error?
A: Because inf is not a valid mathematical result. If you want inf, use overflow="inf".
๐ Credits
Inspired by:
- Universal frustration with
0.1 + 0.2 - Silent bugs in numerical code
- The desire to truly understand how floats work
- A ChatGPT conversation that got out of hand
Made with ๐ฅ and an unhealthy obsession with numerical precision.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nuclear_add-1.0.0.tar.gz.
File metadata
- Download URL: nuclear_add-1.0.0.tar.gz
- Upload date:
- Size: 44.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4c4e375fab6f84041832d8bb4fbb0ea5d37df7834c1b999a5923ef1e6fbb329
|
|
| MD5 |
d5e353e24819b96efb963db57254bbc7
|
|
| BLAKE2b-256 |
8470b12113a6ed097afa39ce5e20819d19ac3ea152e7ab81579ca08dc2acb27b
|
File details
Details for the file nuclear_add-1.0.0-py3-none-any.whl.
File metadata
- Download URL: nuclear_add-1.0.0-py3-none-any.whl
- Upload date:
- Size: 30.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.9.18 {"installer":{"name":"uv","version":"0.9.18","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bbff5e4b925c9200f2c82a20bc335bb63c67b32b7b645da42ca68d6a6fb8701e
|
|
| MD5 |
108d7248c85a2038ae2929da8af13649
|
|
| BLAKE2b-256 |
8fb74fee0c0ddfd9151ffda8bc60b6c27522d2aa9a8100e0d3eafd20a72f0058
|