TheAlgorithms/Python · error · ValueError

Hole concentration cannot be negative in a semiconductor

Error message

Hole concentration cannot be negative in a semiconductor

What it means

Thrown by carrier_concentration() when hole_conc < 0 while exactly one argument is 0. Hole concentration is a physical count per volume and cannot be negative, so the library rejects it before computing the mass-action-law result.

Source

Thrown at electronics/carrier_concentration.py:48

    >>> 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 (
            "intrinsic_conc",
            (electron_conc * hole_conc) ** 0.5,

View on GitHub (pinned to f5988cc097)

Solutions

  1. Correct the sign of hole_conc; valid inputs are non-negative.
  2. Validate measured data before the call and reject/clamp physically impossible negatives at ingestion.
  3. If you meant 'unknown', pass 0 for hole_conc instead of a negative placeholder.

Example fix

# before
carrier_concentration(electron_conc=0, hole_conc=-400, intrinsic_conc=1200)

# after
carrier_concentration(electron_conc=0, hole_conc=400, intrinsic_conc=1200)
Defensive patterns

Strategy: validation

Validate before calling

if hole_conc < 0:
    raise ValueError("hole_conc must be >= 0")
result = carrier_concentration(electron_conc, hole_conc, intrinsic_conc)

Prevention

When it happens

Trigger: carrier_concentration(electron_conc=0, hole_conc=-400, intrinsic_conc=1200) — exactly one zero (electron_conc) but hole_conc is negative.

Common situations: Sign flips when computing p = ni^2/n manually and passing the wrong operand order; negative noise in measured data; spreadsheet columns with stray negative formatting.

Related errors


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