TheAlgorithms/Python · error · ValueError
Electron concentration cannot be negative in a semiconductor
Error message
Electron concentration cannot be negative in a semiconductor
What it means
Thrown by carrier_concentration() when electron_conc < 0 while the zero-count precondition is satisfied. Negative carrier concentrations are non-physical (concentrations are per-volume counts), so the library refuses them before applying the mass-action law n*p = ni^2.
Source
Thrown at electronics/carrier_concentration.py:46
...
ValueError: You cannot supply more or less than 2 values
>>> carrier_concentration(electron_conc=-1000, hole_conc=0, intrinsic_conc=1200)
Traceback (most recent call last):
...
ValueError: Electron concentration cannot be negative in a semiconductor
>>> carrier_concentration(electron_conc=0, hole_conc=-400, intrinsic_conc=1200)
Traceback (most recent call last):
...
ValueError: Hole concentration cannot be negative in a semiconductor
>>> carrier_concentration(electron_conc=0, hole_conc=400, intrinsic_conc=-1200)
Traceback (most recent call last):
...
ValueError: Intrinsic concentration cannot be negative in a semiconductor
"""
if (electron_conc, hole_conc, intrinsic_conc).count(0) != 1:
raise ValueError("You cannot supply more or less than 2 values")
elif electron_conc < 0:
raise ValueError("Electron concentration cannot be negative in a semiconductor")
elif hole_conc < 0:
raise ValueError("Hole concentration cannot be negative in a semiconductor")
elif intrinsic_conc < 0:
raise ValueError(
"Intrinsic concentration cannot be negative in a semiconductor"
)
elif electron_conc == 0:
return (
"electron_conc",
intrinsic_conc**2 / hole_conc,
)
elif hole_conc == 0:
return (
"hole_conc",
intrinsic_conc**2 / electron_conc,
)
elif intrinsic_conc == 0:
return (View on GitHub (pinned to f5988cc097)
Solutions
- Fix the sign of electron_conc at the source; it must be >= 0.
- If the value comes from a noisy measurement, clamp or validate it before calling: raise if electron_conc < 0 rather than letting the library do it.
- Re-derive the value: for n-type material electron_conc should equal N_D (positive donor density).
Example fix
# before carrier_concentration(electron_conc=-1e15, hole_conc=0, intrinsic_conc=1e10) # after carrier_concentration(electron_conc=1e15, hole_conc=0, intrinsic_conc=1e10)
Defensive patterns
Strategy: validation
Validate before calling
if electron_conc < 0:
raise ValueError("electron_conc must be >= 0")
result = carrier_concentration(electron_conc, hole_conc, intrinsic_conc) Prevention
- Carrier concentrations are counts — sanitize with abs() only when sign is a known convention.
- Validate measured data ranges before passing to physics functions.
When it happens
Trigger: carrier_concentration(electron_conc=-100, hole_conc=0, intrinsic_conc=1200) — exactly one zero but a negative electron concentration.
Common situations: Sign errors from subtraction-based derivations (e.g. n = N_D - N_A computed backwards); sensor/instrument data with negative noise around zero; CSV imports where a minus sign slipped in.
Related errors
- Hole concentration cannot be negative in a semiconductor
- Intrinsic concentration cannot be negative in a semiconducto
- Conductivity cannot be negative
- Electron concentration cannot be negative
- mobility cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/5387c94cfa6d4872.
Report an issue: GitHub.