TheAlgorithms/Python · error · ValueError
Inductance cannot be 0 or negative
Error message
Inductance cannot be 0 or negative
What it means
Raised by resonant_frequency() in electronics/resonant_frequency.py when inductance <= 0. The function computes f = 1/(2*pi*sqrt(L*C)), where a non-positive L makes the expression undefined (or a short/degenerate LC tank), so both L and C are required to be strictly positive; L is checked first.
Source
Thrown at electronics/resonant_frequency.py:35
"""
This function can calculate the resonant frequency of LC circuit,
for the given value of inductance and capacitnace.
Examples are given below:
>>> resonant_frequency(inductance=10, capacitance=5)
('Resonant frequency', 0.022507907903927652)
>>> resonant_frequency(inductance=0, capacitance=5)
Traceback (most recent call last):
...
ValueError: Inductance cannot be 0 or negative
>>> resonant_frequency(inductance=10, capacitance=0)
Traceback (most recent call last):
...
ValueError: Capacitance cannot be 0 or negative
"""
if inductance <= 0:
raise ValueError("Inductance cannot be 0 or negative")
elif capacitance <= 0:
raise ValueError("Capacitance cannot be 0 or negative")
else:
return (
"Resonant frequency",
float(1 / (2 * pi * (sqrt(inductance * capacitance)))),
)
if __name__ == "__main__":
import doctest
doctest.testmod()
View on GitHub (pinned to f5988cc097)
Solutions
- Pass a strictly positive inductance in henries, e.g. resonant_frequency(inductance=10, capacitance=5).
- Use keyword arguments so L and C cannot be silently swapped.
- Validate config values at load time: `if cfg.inductance <= 0: raise` with a message naming the config key.
Example fix
# before resonant_frequency(inductance=0, capacitance=5) # ValueError # after resonant_frequency(inductance=10, capacitance=5)
Defensive patterns
Strategy: validation
Validate before calling
if not (inductance > 0):
raise ConfigError("inductance must be > 0 henries") Type guard
def valid_lc(l: float, c: float) -> bool:
return isinstance(l, (int, float)) and isinstance(c, (int, float)) and l > 0 and c > 0 Try / catch
try:
name, freq = resonant_frequency(inductance=L, capacitance=C)
except ValueError as exc:
raise SimulationConfigError(str(exc)) from exc Prevention
- Use keyword arguments for L and C to avoid swaps.
- Fail fast on unset (0-default) config values at load time.
- Keep SI units (henries/farads) end to end.
When it happens
Trigger: resonant_frequency(inductance=0, capacitance=5); passing a negative inductance such as -10; default-initialized variables (0.0) passed because a sensor or config value was never set.
Common situations: Simulation setups where a component is omitted and defaults to 0; parsing configs where the inductance key is missing and a helper returns 0; unit-scale mistakes (e.g. entering 0 because the value was expressed in a scaled unit elsewhere).
Related errors
- Capacitance cannot be 0 or negative
- Capacitor at index {index} has a negative or zero value!
- You cannot supply more or less than 2 values
- Electron concentration cannot be negative in a semiconductor
- Hole concentration cannot be negative in a semiconductor
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/2647c403bf87b80d.
Report an issue: GitHub.