{"record":{"id":"66c155154bd8f5a5","repo":"TheAlgorithms/Python","slug":"capacitance-must-be-positive","errorCode":null,"errorMessage":"Capacitance must be positive.","messagePattern":"Capacitance must be positive\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/charging_capacitor.py","lineNumber":65,"sourceCode":"    ValueError: Source voltage must be positive.\n\n    >>> charging_capacitor(source_voltage=20,resistance=-2000,capacitance=30,time_sec=4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Resistance must be positive.\n\n    >>> charging_capacitor(source_voltage=30,resistance=1500,capacitance=0,time_sec=4)\n    Traceback (most recent call last):\n        ...\n    ValueError: Capacitance must be positive.\n    \"\"\"\n\n    if source_voltage <= 0:\n        raise ValueError(\"Source voltage must be positive.\")\n    if resistance <= 0:\n        raise ValueError(\"Resistance must be positive.\")\n    if capacitance <= 0:\n        raise ValueError(\"Capacitance must be positive.\")\n    return round(source_voltage * (1 - exp(-time_sec / (resistance * capacitance))), 3)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":47,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/charging_capacitor.py#L47-L73","documentation":"Thrown by charging_capacitor() when capacitance <= 0. Capacitance scales the RC time constant; zero or negative capacitance is non-physical for a charging model, so the library refuses it (this is the last of the three sequential guards).","triggerScenarios":"charging_capacitor(source_voltage=30, resistance=1500, capacitance=0, time_sec=4) or any negative capacitance value.","commonSituations":"Unit errors (pF/nF/uF vs F) producing 0 after integer truncation; unset config defaults of 0; passing a component count instead of a farad value.","solutions":["Pass capacitance in farads as a positive number (e.g. 30 as in the doctest, or 1e-6 for 1 uF).","Validate unit conversion before the call; ensure floats, not truncated ints.","Confirm the earlier guards passed — this error implies source_voltage and resistance were already positive."],"exampleFix":"# before\ncharging_capacitor(source_voltage=30, resistance=1500, capacitance=0, time_sec=4)\n\n# after\ncharging_capacitor(source_voltage=30, resistance=1500, capacitance=4.7e-6, time_sec=4)","handlingStrategy":"validation","validationCode":"if capacitance <= 0:\n    raise ValueError(\"capacitance must be positive farads\")\nv = charging_capacitor(source_voltage, resistance, capacitance, time_sec)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Convert uF/nF/pF to farads with explicit multipliers (1e-6, 1e-9, 1e-12).","Avoid integer truncation of small fractional farad values."],"tags":["electronics","rc-circuit","physics","validation","unit-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}