{"record":{"id":"bcb0fa49a2a0d0b6","repo":"TheAlgorithms/Python","slug":"inductance-cannot-be-negative","errorCode":null,"errorMessage":"Inductance cannot be negative","messagePattern":"Inductance cannot be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/ind_reactance.py","lineNumber":51,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: Inductive reactance cannot be negative\n\n    >>> ind_reactance(0, 10e3, 50)\n    {'inductance': 0.0007957747154594767}\n\n    >>> ind_reactance(35e-3, 0, 50)\n    {'frequency': 227.36420441699332}\n\n    >>> ind_reactance(35e-6, 1e3, 0)\n    {'reactance': 0.2199114857512855}\n\n    \"\"\"\n\n    if (inductance, frequency, reactance).count(0) != 1:\n        raise ValueError(\"One and only one argument must be 0\")\n    if inductance < 0:\n        raise ValueError(\"Inductance cannot be negative\")\n    if frequency < 0:\n        raise ValueError(\"Frequency cannot be negative\")\n    if reactance < 0:\n        raise ValueError(\"Inductive reactance cannot be negative\")\n    if inductance == 0:\n        return {\"inductance\": reactance / (2 * pi * frequency)}\n    elif frequency == 0:\n        return {\"frequency\": reactance / (2 * pi * inductance)}\n    elif reactance == 0:\n        return {\"reactance\": 2 * pi * frequency * inductance}\n    else:\n        raise ValueError(\"Exactly one argument must be 0\")\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod()","sourceCodeStart":33,"sourceCodeEnd":69,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/ind_reactance.py#L33-L69","documentation":"Thrown by ind_reactance() when the inductance argument is negative. After the exactly-one-zero check passes, the function rejects negative physical quantities; inductance in henries cannot be negative, and the guard prevents computing meaningless reactance/frequency results from a bad magnitude.","triggerScenarios":"ind_reactance(-35e-3, 1e3, 0) or ind_reactance(-0.01, 0, 50) — any call where inductance < 0 and exactly one argument is 0.","commonSituations":"Sign errors in unit conversions (mH to H scaling with a negative factor); raw instrument values where a minus sign indicates direction being passed through; upstream arithmetic producing a negative intermediate.","solutions":["Pass a non-negative inductance in henries (e.g. 35e-3 for 35 mH).","Use abs() at the call site only if the sign is known to be spurious direction metadata.","Trace and fix the upstream computation if a legitimately positive quantity arrives negative."],"exampleFix":"# before\nind_reactance(-35e-3, 1e3, 0)  # ValueError: Inductance cannot be negative\n\n# after\nind_reactance(abs(-35e-3), 1e3, 0)  # {'reactance': 0.2199114857512855}","handlingStrategy":"validation","validationCode":"if inductance < 0:\n    raise ValueError('inductance must be >= 0')","typeGuard":"def valid_inductance(l: float) -> bool:\n    return l >= 0","tryCatchPattern":"try:\n    out = ind_reactance(l, f, x)\nexcept ValueError as exc:\n    if 'Inductance cannot be negative' in str(exc):\n        out = ind_reactance(abs(l), f, x)  # only if sign is known-spurious\n    else:\n        raise","preventionTips":["Keep inductance in henries, non-negative.","Fix sign-flipping unit conversions upstream.","Only abs() when the minus sign is direction metadata, never silently."],"tags":["electronics","validation","inductance","reactance","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}