"""
Generated by EML-lang Python backend.
Source module: aerospace_actuator_guard_band
Kernel digest: sha256:0b0ef5a5baecb59acc7d28dc64bc959d9f3378cfbc278204c737cef5bc755aaa
Functions:     1
Constants:     0
"""

from __future__ import annotations

import math

def _eml_builtin_max(a, b):
    # EML max: NaN if either argument is NaN; Python's max(1.0, nan) is 1.0.
    if a != a: return a
    if b != b: return b
    if a < b: return b
    if b < a: return a
    # a == b is the SIGNED ZERO case: IEEE 754-2019 maximum returns the POSITIVE zero,
    # where Python's max returns its FIRST argument on a tie (max(-0.0, 0.0) was -0.0,
    # measured by bits). (-0)+(+0) is +0 and (-0)+(-0) is -0, so a + b IS the maximum
    # of two zeros -- and it needs no `math`, which an artifact may not import.
    return a + b if a == 0.0 else a

def _eml_builtin_min(a, b):
    # EML min: NaN if either argument is NaN; Python's min(1.0, nan) is 1.0.
    if a != a: return a
    if b != b: return b
    if a < b: return a
    if b < a: return b
    # a == b is the SIGNED ZERO case: IEEE 754-2019 minimum returns the NEGATIVE zero,
    # where Python's min returns its FIRST argument on a tie (min(0.0, -0.0) was 0.0,
    # measured by bits). -((-a) + (-b)) is -0.0 unless both are +0.0.
    return -((-a) + (-b)) if a == 0.0 else a

# actuator_command_within_band
# Pfaffian chain count (eml-cost pfaffian_r): 0    Cost class: p0-d4-w0-c0    EML depth: 4
def actuator_command_within_band(error, integral, deriv, kp, ki, kd, u_min, u_max):
    if not ((u_min <= u_max)): raise ValueError('actuator_command_within_band: requires ((u_min <= u_max))')
    # source obligations for actuator_command_within_band: {O1, O2}
    #   O1 [43d7c3ff9568] -> PRESERVED (equivalent)  ValueError before return
    #        build: unconditional -- raise ValueError, NOT `assert` -- python -O strips assert statements and not raises
    #   O2 [ca2e303109b7] -> PRESERVED (equivalent)  ValueError before return
    #        build: unconditional -- raise ValueError, NOT `assert` -- python -O strips assert statements and not raises
    result = _eml_builtin_max(u_min, _eml_builtin_min((((kp * error) + (ki * integral)) + (kd * deriv)), u_max))
    if not ((u_min <= result)): raise ValueError('actuator_command_within_band: ensures violated')
    if not ((result <= u_max)): raise ValueError('actuator_command_within_band: ensures violated')
    return result
