TheAlgorithms/Python · error · ValueError
Electron concentration cannot be negative
Error message
Electron concentration cannot be negative
What it means
Thrown by electric_conductivity() when electron_conc < 0 while exactly one argument is 0. Electron concentration is a per-volume count and cannot be negative; the guard runs before the solver branch that divides by (mobility * ELECTRON_CHARGE).
Source
Thrown at electronics/electric_conductivity.py:50
>>> electric_conductivity(conductivity=50, electron_conc=0, mobility=-10)
Traceback (most recent call last):
...
ValueError: mobility cannot be negative
>>> electric_conductivity(conductivity=50, electron_conc=0, mobility=0)
Traceback (most recent call last):
...
ValueError: You cannot supply more or less than 2 values
>>> electric_conductivity(conductivity=50, electron_conc=200, mobility=300)
Traceback (most recent call last):
...
ValueError: You cannot supply more or less than 2 values
"""
if (conductivity, electron_conc, mobility).count(0) != 1:
raise ValueError("You cannot supply more or less than 2 values")
elif conductivity < 0:
raise ValueError("Conductivity cannot be negative")
elif electron_conc < 0:
raise ValueError("Electron concentration cannot be negative")
elif mobility < 0:
raise ValueError("mobility cannot be negative")
elif conductivity == 0:
return (
"conductivity",
mobility * electron_conc * ELECTRON_CHARGE,
)
elif electron_conc == 0:
return (
"electron_conc",
conductivity / (mobility * ELECTRON_CHARGE),
)
else:
return (
"mobility",
conductivity / (electron_conc * ELECTRON_CHARGE),
)
View on GitHub (pinned to f5988cc097)
Solutions
- Pass the magnitude of the electron concentration as a non-negative number.
- If your codebase signs carriers by polarity, strip the sign at the boundary: abs(n).
- Mark the unknown with 0 rather than any placeholder value.
Example fix
# before electric_conductivity(conductivity=50, electron_conc=-200, mobility=0) # after electric_conductivity(conductivity=50, electron_conc=abs(carrier_density), mobility=0)
Defensive patterns
Strategy: validation
Validate before calling
n = abs(carrier_density) if signed_convention else carrier_density
if n < 0:
raise ValueError("electron_conc must be >= 0")
name, value = electric_conductivity(conductivity, n, mobility) Prevention
- Strip carrier-sign conventions at the boundary when magnitudes are required.
- Validate doping arithmetic (N_D - N_A) order.
When it happens
Trigger: electric_conductivity(conductivity=50, electron_conc=-200, mobility=0) — one zero but negative electron concentration.
Common situations: Charge-carrier bookkeeping where electrons were tracked with a negative sign by convention; noisy sensor data; doping calculations with operands swapped (N_D - N_A reversed).
Related errors
- Electron concentration cannot be negative in a semiconductor
- Hole concentration cannot be negative in a semiconductor
- Intrinsic concentration cannot be negative in a semiconducto
- Conductivity cannot be negative
- mobility cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5fecaf7851320799.
Report an issue: GitHub.