Skip to main content

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/ :

๐Ÿ› ๏ธ 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nuclear_add-1.0.0.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

nuclear_add-1.0.0-py3-none-any.whl (30.9 kB view details)

Uploaded Python 3

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

Hashes for nuclear_add-1.0.0.tar.gz
Algorithm Hash digest
SHA256 a4c4e375fab6f84041832d8bb4fbb0ea5d37df7834c1b999a5923ef1e6fbb329
MD5 d5e353e24819b96efb963db57254bbc7
BLAKE2b-256 8470b12113a6ed097afa39ce5e20819d19ac3ea152e7ab81579ca08dc2acb27b

See more details on using hashes here.

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

Hashes for nuclear_add-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 bbff5e4b925c9200f2c82a20bc335bb63c67b32b7b645da42ca68d6a6fb8701e
MD5 108d7248c85a2038ae2929da8af13649
BLAKE2b-256 8fb74fee0c0ddfd9151ffda8bc60b6c27522d2aa9a8100e0d3eafd20a72f0058

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page