TheAlgorithms/Python · error · ValueError
Donor concentration should be positive
Error message
Donor concentration should be positive
What it means
Raised by builtin_voltage() (electronics/builtin_voltage.py:42) when donor_conc <= 0. The function computes a PN-junction built-in voltage V_bi = kT/q * ln(Nd*Na/ni^2), which is only physically meaningful for positive doping concentrations. This is the first check in an ordered elif chain, so it fires before the acceptor/intrinsic checks.
Source
Thrown at electronics/builtin_voltage.py:42
Traceback (most recent call last):
...
ValueError: Acceptor concentration should be positive
>>> builtin_voltage(donor_conc=1000, acceptor_conc=1000, intrinsic_conc=0)
Traceback (most recent call last):
...
ValueError: Intrinsic concentration should be positive
>>> builtin_voltage(donor_conc=1000, acceptor_conc=3000, intrinsic_conc=2000)
Traceback (most recent call last):
...
ValueError: Donor concentration should be greater than intrinsic concentration
>>> builtin_voltage(donor_conc=3000, acceptor_conc=1000, intrinsic_conc=2000)
Traceback (most recent call last):
...
ValueError: Acceptor concentration should be greater than intrinsic concentration
"""
if donor_conc <= 0:
raise ValueError("Donor concentration should be positive")
elif acceptor_conc <= 0:
raise ValueError("Acceptor concentration should be positive")
elif intrinsic_conc <= 0:
raise ValueError("Intrinsic concentration should be positive")
elif donor_conc <= intrinsic_conc:
raise ValueError(
"Donor concentration should be greater than intrinsic concentration"
)
elif acceptor_conc <= intrinsic_conc:
raise ValueError(
"Acceptor concentration should be greater than intrinsic concentration"
)
else:
return (
Boltzmann
* T
* log((donor_conc * acceptor_conc) / intrinsic_conc**2)
/ physical_constants["electron volt"][0]View on GitHub (pinned to f5988cc097)
Solutions
- Pass positive physical values, typically ~1e15 to 1e19 per cm^3 for silicon.
- Check the value's origin: if it comes from a parser, treat 0/None as missing data and fail loudly upstream.
- Verify unit conversions use float math (1e6 factors, not int).
Example fix
# before builtin_voltage(donor_conc=0, acceptor_conc=1e16, intrinsic_conc=1e10) # ValueError # after builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)
Defensive patterns
Strategy: validation
Validate before calling
def positive(x: float) -> bool:
return isinstance(x, (int, float)) and x > 0 Type guard
def positive_float(x: object) -> TypeGuard[float]:
return isinstance(x, (int, float)) and not isinstance(x, bool) and x > 0 Try / catch
try:
builtin_voltage(nd, na, ni)
except ValueError as e:
if 'positive' in str(e):
raise ValueError(f'Bad doping input: nd={nd}, na={na}, ni={ni}') from e
raise Prevention
- Default concentrations to None and validate, never to 0.
- Use float literals for unit conversions (1e6, not integer math).
- Range-check doping values (1e12-1e21 cm^-3) at ingest.
When it happens
Trigger: Calling builtin_voltage(donor_conc=0, ...), with a negative donor concentration, or with 0.0 floats. Only fires when donor_conc is the first failing value; a negative acceptor_conc with positive donor_conc raises the acceptor error instead.
Common situations: Default-initializing concentration variables to 0; unit-conversion bugs (e.g. cm^-3 to m^-3 producing 0 via integer division); reading missing values from a datasheet column that default to 0.
Related errors
- Acceptor concentration should be positive
- Intrinsic concentration should be positive
- Capacitor at index {index} has a negative value!
- Capacitor at index {index} has a negative or zero value!
- Expected a_coeffs to have {self.order + 1} elements for {sel
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/a1139940d74fd72b.
Report an issue: GitHub.