TheAlgorithms/Python · error · ValueError

Intrinsic concentration should be positive

Error message

Intrinsic concentration should be positive

What it means

Raised by builtin_voltage() when intrinsic_conc <= 0 while donor and acceptor concentrations are already positive. ni appears squared in the denominator of the log argument, and ni <= 0 is non-physical (silicon at 300K has ni ≈ 1e10 cm^-3). Third check in the ordered elif chain.

Source

Thrown at electronics/builtin_voltage.py:46

    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]
        )


if __name__ == "__main__":

View on GitHub (pinned to f5988cc097)

Solutions

  1. Use a physically correct ni (e.g. 1e10 cm^-3 or 1.45e10 depending on convention; 1.5e10 in the module's doctest style).
  2. If ni is computed from temperature, guard the formula's valid range and check the result > 0.
  3. Keep units consistent — ni, Nd, Na must be in the same volume units.

Example fix

# before
builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=0)  # ValueError

# after
builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)
Defensive patterns

Strategy: validation

Validate before calling

def valid_intrinsic(ni: float) -> bool:
    return ni > 0  # silicon at 300K: ~1e10 cm^-3

Prevention

When it happens

Trigger: Calling builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=0) or intrinsic_conc=-1e10. Fires only when both donor_conc > 0 and acceptor_conc > 0.

Common situations: Omitting intrinsic_conc and having it default/decay to 0; computing ni from a temperature-dependent formula that underflows to 0.0 at low T; using ni for a different material where the value was never set.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/70fea80404282d27. Report an issue: GitHub.