{"record":{"id":"37c7e702689fc679","repo":"TheAlgorithms/Python","slug":"donor-concentration-should-be-greater-than-intrins","errorCode":null,"errorMessage":"Donor concentration should be greater than intrinsic concentration","messagePattern":"Donor concentration should be greater than intrinsic concentration","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/builtin_voltage.py","lineNumber":48,"sourceCode":"    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]\n        )\n\n\nif __name__ == \"__main__\":\n    import doctest\n","sourceCodeStart":30,"sourceCodeEnd":66,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/builtin_voltage.py#L30-L66","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check unit consistency first — convert all three to the same units (e.g. cm^-3): 1 m^-3 = 1e-6 cm^-3.","Use realistic silicon values: Nd and Na of 1e15-1e19 cm^-3 vs ni ≈ 1e10 cm^-3.","If the doping really is near-intrinsic, the built-in-voltage model itself is inappropriate — reconsider the calculation."],"exampleFix":"# before\n# donor in m^-3, others in cm^-3 -> unit clash\nbuiltin_voltage(donor_conc=1e22, acceptor_conc=1e15, intrinsic_conc=1e10)  # may still pass wrongly\n\n# after\n# all in cm^-3\nbuiltin_voltage(donor_conc=1e16, acceptor_conc=1e15, intrinsic_conc=1e10)","handlingStrategy":"validation","validationCode":"def valid_junction(nd: float, na: float, ni: float) -> bool:\n    return nd > 0 and na > 0 and ni > 0 and nd > ni and na > ni","typeGuard":null,"tryCatchPattern":"try:\n    builtin_voltage(nd, na, ni)\nexcept ValueError as e:\n    if 'greater than intrinsic' in str(e):\n        # likely unit mismatch: normalize all to cm^-3 and retry once\n        raise ValueError(f'Check units: nd={nd}, na={na}, ni={ni}') from e\n    raise","preventionTips":["Convert all concentrations to one unit system (cm^-3) before the call.","Sanity-check that Nd and Na are several orders above ni (~1e10 for Si).","If doping is near-intrinsic, use a different model — this formula's precondition fails."],"tags":["electronics","physics","units"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}