TheAlgorithms/Python · error · ValueError

Donor concentration should be greater than intrinsic concent

Error message

Donor concentration should be greater than intrinsic concentration

What it means

Raised by builtin_voltage() when donor_conc <= intrinsic_conc (both positive). A valid junction requires Nd > ni; otherwise ln(Nd*Na/ni^2) <= ln(Na) <= 0 territory and the built-in potential formula loses meaning for this N-type reasoning. Fourth check — all three concentrations must already be positive.

Source

Thrown at electronics/builtin_voltage.py:48

    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__":
    import doctest

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check unit consistency first — convert all three to the same units (e.g. cm^-3): 1 m^-3 = 1e-6 cm^-3.
  2. Use realistic silicon values: Nd and Na of 1e15-1e19 cm^-3 vs ni ≈ 1e10 cm^-3.
  3. If the doping really is near-intrinsic, the built-in-voltage model itself is inappropriate — reconsider the calculation.

Example fix

# before
# donor in m^-3, others in cm^-3 -> unit clash
builtin_voltage(donor_conc=1e22, acceptor_conc=1e15, intrinsic_conc=1e10)  # may still pass wrongly

# after
# all in cm^-3
builtin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)
Defensive patterns

Strategy: validation

Validate before calling

def valid_junction(nd: float, na: float, ni: float) -> bool:
    return nd > 0 and na > 0 and ni > 0 and nd > ni and na > ni

Try / catch

try:
    builtin_voltage(nd, na, ni)
except ValueError as e:
    if 'greater than intrinsic' in str(e):
        # likely unit mismatch: normalize all to cm^-3 and retry once
        raise ValueError(f'Check units: nd={nd}, na={na}, ni={ni}') from e
    raise

Prevention

When it happens

Trigger: Calling builtin_voltage(donor_conc=1000, acceptor_conc=3000, intrinsic_conc=2000) exactly as in the doctest; any case where 0 < donor_conc <= intrinsic_conc < acceptor_conc's check. Equality (donor_conc == intrinsic_conc) also triggers it.

Common situations: Unit mismatch: Nd in m^-3 (1e22) vs ni in cm^-3 (1e10) or vice versa, making one appear smaller; forgetting that ni for silicon is ~1e10 and passing raw exponent-scaled values; using intrinsic-semiconductor-level doping where Nd ~ ni.

Related errors


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