{"record":{"id":"5387c94cfa6d4872","repo":"TheAlgorithms/Python","slug":"electron-concentration-cannot-be-negative-in-a-sem","errorCode":null,"errorMessage":"Electron concentration cannot be negative in a semiconductor","messagePattern":"Electron concentration cannot be negative in a semiconductor","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/carrier_concentration.py","lineNumber":46,"sourceCode":"        ...\n    ValueError: You cannot supply more or less than 2 values\n    >>> carrier_concentration(electron_conc=-1000, hole_conc=0, intrinsic_conc=1200)\n    Traceback (most recent call last):\n        ...\n    ValueError: Electron concentration cannot be negative in a semiconductor\n    >>> carrier_concentration(electron_conc=0, hole_conc=-400, intrinsic_conc=1200)\n    Traceback (most recent call last):\n        ...\n    ValueError: Hole concentration cannot be negative in a semiconductor\n    >>> carrier_concentration(electron_conc=0, hole_conc=400, intrinsic_conc=-1200)\n    Traceback (most recent call last):\n        ...\n    ValueError: Intrinsic concentration cannot be negative in a semiconductor\n    \"\"\"\n    if (electron_conc, hole_conc, intrinsic_conc).count(0) != 1:\n        raise ValueError(\"You cannot supply more or less than 2 values\")\n    elif electron_conc < 0:\n        raise ValueError(\"Electron concentration cannot be negative in a semiconductor\")\n    elif hole_conc < 0:\n        raise ValueError(\"Hole concentration cannot be negative in a semiconductor\")\n    elif intrinsic_conc < 0:\n        raise ValueError(\n            \"Intrinsic concentration cannot be negative in a semiconductor\"\n        )\n    elif electron_conc == 0:\n        return (\n            \"electron_conc\",\n            intrinsic_conc**2 / hole_conc,\n        )\n    elif hole_conc == 0:\n        return (\n            \"hole_conc\",\n            intrinsic_conc**2 / electron_conc,\n        )\n    elif intrinsic_conc == 0:\n        return (","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/carrier_concentration.py#L28-L64","documentation":"Thrown by carrier_concentration() when electron_conc < 0 while the zero-count precondition is satisfied. Negative carrier concentrations are non-physical (concentrations are per-volume counts), so the library refuses them before applying the mass-action law n*p = ni^2.","triggerScenarios":"carrier_concentration(electron_conc=-100, hole_conc=0, intrinsic_conc=1200) — exactly one zero but a negative electron concentration.","commonSituations":"Sign errors from subtraction-based derivations (e.g. n = N_D - N_A computed backwards); sensor/instrument data with negative noise around zero; CSV imports where a minus sign slipped in.","solutions":["Fix the sign of electron_conc at the source; it must be >= 0.","If the value comes from a noisy measurement, clamp or validate it before calling: raise if electron_conc < 0 rather than letting the library do it.","Re-derive the value: for n-type material electron_conc should equal N_D (positive donor density)."],"exampleFix":"# before\ncarrier_concentration(electron_conc=-1e15, hole_conc=0, intrinsic_conc=1e10)\n\n# after\ncarrier_concentration(electron_conc=1e15, hole_conc=0, intrinsic_conc=1e10)","handlingStrategy":"validation","validationCode":"if electron_conc < 0:\n    raise ValueError(\"electron_conc must be >= 0\")\nresult = carrier_concentration(electron_conc, hole_conc, intrinsic_conc)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Carrier concentrations are counts — sanitize with abs() only when sign is a known convention.","Validate measured data ranges before passing to physics functions."],"tags":["electronics","semiconductor","physics","validation","sign-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}