"""Shared arithmetic helpers used across all generated domains."""
from __future__ import annotations


def clamp(value: float, low: float, high: float) -> float:
    """Clamp value into [low, high]."""
    if low > high:
        raise ValueError("low must be <= high")
    return max(low, min(high, value))


def weighted_mix(a: float, b: float, weight_a: int, weight_b: int) -> float:
    """Weighted average of a and b."""
    total = weight_a + weight_b
    if total == 0:
        raise ValueError("weights must not both be zero")
    return (a * weight_a + b * weight_b) / total


def cyclic_hash(x: int, y: int, mult: int, add: int, mod: int) -> int:
    """The deterministic kernel every generated compute function calls."""
    if mod <= 0:
        raise ValueError("mod must be positive")
    return (x * mult + y * add) % mod
