{"record":{"id":"a1139940d74fd72b","repo":"TheAlgorithms/Python","slug":"donor-concentration-should-be-positive","errorCode":null,"errorMessage":"Donor concentration should be positive","messagePattern":"Donor concentration should be positive","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/builtin_voltage.py","lineNumber":42,"sourceCode":"    Traceback (most recent call last):\n      ...\n    ValueError: Acceptor concentration should be positive\n    >>> builtin_voltage(donor_conc=1000, acceptor_conc=1000, intrinsic_conc=0)\n    Traceback (most recent call last):\n      ...\n    ValueError: Intrinsic concentration should be positive\n    >>> builtin_voltage(donor_conc=1000, acceptor_conc=3000, intrinsic_conc=2000)\n    Traceback (most recent call last):\n      ...\n    ValueError: Donor concentration should be greater than intrinsic concentration\n    >>> builtin_voltage(donor_conc=3000, acceptor_conc=1000, intrinsic_conc=2000)\n    Traceback (most recent call last):\n      ...\n    ValueError: Acceptor concentration should be greater than intrinsic concentration\n    \"\"\"\n\n    if donor_conc <= 0:\n        raise ValueError(\"Donor concentration should be positive\")\n    elif acceptor_conc <= 0:\n        raise ValueError(\"Acceptor concentration should be positive\")\n    elif intrinsic_conc <= 0:\n        raise ValueError(\"Intrinsic concentration should be positive\")\n    elif donor_conc <= intrinsic_conc:\n        raise ValueError(\n            \"Donor concentration should be greater than intrinsic concentration\"\n        )\n    elif acceptor_conc <= intrinsic_conc:\n        raise ValueError(\n            \"Acceptor concentration should be greater than intrinsic concentration\"\n        )\n    else:\n        return (\n            Boltzmann\n            * T\n            * log((donor_conc * acceptor_conc) / intrinsic_conc**2)\n            / physical_constants[\"electron volt\"][0]","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/builtin_voltage.py#L24-L60","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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)."],"exampleFix":"# before\nbuiltin_voltage(donor_conc=0, acceptor_conc=1e16, intrinsic_conc=1e10)  # ValueError\n\n# after\nbuiltin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)","handlingStrategy":"validation","validationCode":"def positive(x: float) -> bool:\n    return isinstance(x, (int, float)) and x > 0","typeGuard":"def positive_float(x: object) -> TypeGuard[float]:\n    return isinstance(x, (int, float)) and not isinstance(x, bool) and x > 0","tryCatchPattern":"try:\n    builtin_voltage(nd, na, ni)\nexcept ValueError as e:\n    if 'positive' in str(e):\n        raise ValueError(f'Bad doping input: nd={nd}, na={na}, ni={ni}') from e\n    raise","preventionTips":["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."],"tags":["electronics","physics","input-validation"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}