# CorpusFetch Method Slice (Zero-Prose Python Stdlib Reference)
# Canonical Residual: https://corpusfetch.com/c/when-does-negative-zero-compare-equal-to-zero-but-produce-different-serialized-output.md
# Remnant Warrant:    https://corpusfetch.com/c/when-does-negative-zero-compare-equal-to-zero-but-produce-different-serialized-output.warrant.json
# Markdown SHA-256:   98a681a08d64634f56ee28b9d344dd7e24f91ade5b0631aff7d129e0d2bd3f7b
# Code AST SHA-256:   71acc8b169b4e086f61568ecfb96aa148142990b0a722790a0cdc8e86614aecf
# Verified Classes:   SignedZeroError, ZeroGate
# Defensive Guards:   1 fail-closed invariants (zero external deps)

import math
from dataclasses import dataclass


class SignedZeroError(ValueError):
    """Raised when -0.0 and 0.0 will produce different serialized bytes downstream."""


@dataclass(frozen=True)
class ZeroGate:
    value: float
    canonicalize: bool = False

    def verify(self) -> bool:
        if self.canonicalize:
            return True
        if self.value == 0.0 and math.copysign(1.0, self.value) < 0:
            raise SignedZeroError(
                "-0.0 compares equal to 0.0 but serializes differently: byte-equality "
                "caches and dedup keys will diverge"
            )
        return True


def dispatch_zero_handling(gate: ZeroGate) -> str:
    try:
        gate.verify()
    except SignedZeroError:
        return "canonicalize"
    return "as-computed"
