TheAlgorithms/Python · error · ValueError

Inductive reactance cannot be negative

Error message

Inductive reactance cannot be negative

What it means

Thrown by ind_reactance() when the reactance argument is negative. Inductive reactance X_L = 2*pi*f*L is non-negative for non-negative f and L, so a negative reactance input is rejected before the function solves for inductance or frequency.

Source

Thrown at electronics/ind_reactance.py:55

    >>> ind_reactance(0, 10e3, 50)
    {'inductance': 0.0007957747154594767}

    >>> ind_reactance(35e-3, 0, 50)
    {'frequency': 227.36420441699332}

    >>> ind_reactance(35e-6, 1e3, 0)
    {'reactance': 0.2199114857512855}

    """

    if (inductance, frequency, reactance).count(0) != 1:
        raise ValueError("One and only one argument must be 0")
    if inductance < 0:
        raise ValueError("Inductance cannot be negative")
    if frequency < 0:
        raise ValueError("Frequency cannot be negative")
    if reactance < 0:
        raise ValueError("Inductive reactance cannot be negative")
    if inductance == 0:
        return {"inductance": reactance / (2 * pi * frequency)}
    elif frequency == 0:
        return {"frequency": reactance / (2 * pi * inductance)}
    elif reactance == 0:
        return {"reactance": 2 * pi * frequency * inductance}
    else:
        raise ValueError("Exactly one argument must be 0")


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass reactance as a non-negative magnitude; use abs(reactance) if the sign only encoded inductive/capacitive kind.
  2. If your source uses signed reactance, convert first: negative means capacitive — consider cap_reactance-style handling instead.
  3. Guard reactance >= 0 in input validation before calling.

Example fix

# before
ind_reactance(0, 1e3, -50)  # ValueError: Inductive reactance cannot be negative

# after
ind_reactance(0, 1e3, abs(-50))  # {'inductance': 0.007957747154594767}
Defensive patterns

Strategy: validation

Validate before calling

if reactance < 0:
    raise ValueError('inductive reactance must be >= 0')

Type guard

def valid_inductive_reactance(x: float) -> bool:
    return x >= 0

Try / catch

try:
    out = ind_reactance(l, f, x)
except ValueError as exc:
    if 'reactance cannot be negative' in str(exc):
        # negative reactance usually means CAPACITIVE: route accordingly
        ...
    raise

Prevention

When it happens

Trigger: ind_reactance(0, 1e3, -50) or ind_reactance(35e-3, 0, -10) — any call where reactance < 0 and exactly one argument equals 0.

Common situations: Feeding impedance-meter output where a negative sign denotes capacitive vs inductive reactance; reusing values computed for capacitive reactance (X_C = 1/(2*pi*f*C)) which can carry sign conventions; data-entry sign errors.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/6f8768c0bd339c33. Report an issue: GitHub.