# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-does-float-accumulation-over-a-ledger-diverge-from-decimal-arithmetic.md
# Remnant Warrant:    https://corpusfetch.com/c/when-does-float-accumulation-over-a-ledger-diverge-from-decimal-arithmetic.warrant.json
# Markdown SHA-256:   7467d9489b791ec473b917ff559f3801650058e7acdc70e2e1e65608de65d5e8
# Code AST SHA-256:   15e42ba3bea90697e87f7ce6881cd83df9b9cad55e1ed35664cda107b8fbaa54
# Verified Classes:   LedgerPrecisionError, LedgerGate
# Defensive Guards:   2 fail-closed invariants (zero external deps)

from dataclasses import dataclass
from decimal import Decimal


class LedgerPrecisionError(ValueError):
    """Raised when a monetary accumulator cannot represent its sum exactly."""


@dataclass(frozen=True)
class LedgerGate:
    representation: str  # "integer-minor", "decimal", "float"
    max_additions: int = 10 ** 6
    resolution_units: int = 1

    def verify(self) -> bool:
        if self.representation in ("integer-minor", "decimal"):
            return True
        if self.representation != "float":
            raise LedgerPrecisionError(f"unknown representation {self.representation!r}")
        raise LedgerPrecisionError(
            f"float accumulator over {self.max_additions} additions cannot "
            f"guarantee resolution {self.resolution_units}"
        )


def dispatch_accumulator(gate: LedgerGate):
    try:
        gate.verify()
    except LedgerPrecisionError:
        return Decimal
    return float
