TheAlgorithms/Python · error · ValueError
Inductance cannot be negative
Error message
Inductance cannot be negative
What it means
Thrown by ind_reactance() when the inductance argument is negative. After the exactly-one-zero check passes, the function rejects negative physical quantities; inductance in henries cannot be negative, and the guard prevents computing meaningless reactance/frequency results from a bad magnitude.
Source
Thrown at electronics/ind_reactance.py:51
Traceback (most recent call last):
...
ValueError: Inductive reactance cannot be negative
>>> 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
- Pass a non-negative inductance in henries (e.g. 35e-3 for 35 mH).
- Use abs() at the call site only if the sign is known to be spurious direction metadata.
- Trace and fix the upstream computation if a legitimately positive quantity arrives negative.
Example fix
# before
ind_reactance(-35e-3, 1e3, 0) # ValueError: Inductance cannot be negative
# after
ind_reactance(abs(-35e-3), 1e3, 0) # {'reactance': 0.2199114857512855} Defensive patterns
Strategy: validation
Validate before calling
if inductance < 0:
raise ValueError('inductance must be >= 0') Type guard
def valid_inductance(l: float) -> bool:
return l >= 0 Try / catch
try:
out = ind_reactance(l, f, x)
except ValueError as exc:
if 'Inductance cannot be negative' in str(exc):
out = ind_reactance(abs(l), f, x) # only if sign is known-spurious
else:
raise Prevention
- Keep inductance in henries, non-negative.
- Fix sign-flipping unit conversions upstream.
- Only abs() when the minus sign is direction metadata, never silently.
When it happens
Trigger: ind_reactance(-35e-3, 1e3, 0) or ind_reactance(-0.01, 0, 50) — any call where inductance < 0 and exactly one argument is 0.
Common situations: Sign errors in unit conversions (mH to H scaling with a negative factor); raw instrument values where a minus sign indicates direction being passed through; upstream arithmetic producing a negative intermediate.
Related errors
- One and only one argument must be 0
- Frequency cannot be negative
- Inductive reactance cannot be negative
- Power cannot be negative in any electrical/electronics syste
- One and only one argument must be 0
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/bcb0fa49a2a0d0b6.
Report an issue: GitHub.