{"record":{"id":"291fb690b2cbee9b","repo":"TheAlgorithms/Python","slug":"capacitance-cannot-be-0-or-negative","errorCode":null,"errorMessage":"Capacitance cannot be 0 or negative","messagePattern":"Capacitance cannot be 0 or negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/resonant_frequency.py","lineNumber":38,"sourceCode":"\n    Examples are given below:\n    >>> resonant_frequency(inductance=10, capacitance=5)\n    ('Resonant frequency', 0.022507907903927652)\n    >>> resonant_frequency(inductance=0, capacitance=5)\n    Traceback (most recent call last):\n      ...\n    ValueError: Inductance cannot be 0 or negative\n    >>> resonant_frequency(inductance=10, capacitance=0)\n    Traceback (most recent call last):\n      ...\n    ValueError: Capacitance cannot be 0 or negative\n    \"\"\"\n\n    if inductance <= 0:\n        raise ValueError(\"Inductance cannot be 0 or negative\")\n\n    elif capacitance <= 0:\n        raise ValueError(\"Capacitance cannot be 0 or negative\")\n\n    else:\n        return (\n            \"Resonant frequency\",\n            float(1 / (2 * pi * (sqrt(inductance * capacitance)))),\n        )\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()\n","sourceCodeStart":20,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/resonant_frequency.py#L20-L51","documentation":"Raised by resonant_frequency() in electronics/resonant_frequency.py when capacitance <= 0 (the inductance check runs first, so this fires only when L is already valid). Same rationale as the L check: f = 1/(2*pi*sqrt(L*C)) is undefined for C <= 0, and a zero capacitor means no LC tank exists.","triggerScenarios":"resonant_frequency(inductance=10, capacitance=0); negative capacitance from a bad parse; swapping arguments positionally so a 0 meant for another parameter lands in capacitance.","commonSituations":"Permutations of positional args (L and C are both plain floats, so a swap type-checks fine); optional capacitance defaulting to 0 in a GUI form; tolerance deltas subtracted down to or below zero.","solutions":["Pass a strictly positive capacitance in farads.","Call with keywords (inductance=..., capacitance=...) to avoid positional swaps.","Pre-check both values: `assert L > 0 and C > 0` before the call in test/setup code."],"exampleFix":"# before\nresonant_frequency(10, 0)  # ValueError: Capacitance cannot be 0 or negative\n\n# after\nfreq = resonant_frequency(inductance=10, capacitance=5)","handlingStrategy":"validation","validationCode":"if not (capacitance > 0):\n    raise ConfigError(\"capacitance must be > 0 farads\")","typeGuard":"def valid_capacitance(c) -> bool:\n    return isinstance(c, (int, float)) and c > 0","tryCatchPattern":"try:\n    _, freq = resonant_frequency(inductance=L, capacitance=C)\nexcept ValueError as exc:\n    # inductance is checked first; if you get here, L was fine and C is bad\n    raise SimulationConfigError(str(exc)) from exc","preventionTips":["Never pass L and C positionally when refactoring.","Require explicit values (no 0 defaults) for tank components in configs.","Unit-scale check: pF/nF values must be converted to farads."],"tags":["electronics","lc-circuit","validation","physics"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}