{"record":{"id":"285fb53e21ca05e2","repo":"TheAlgorithms/Python","slug":"distance-cannot-be-negative","errorCode":null,"errorMessage":"Distance cannot be negative","messagePattern":"Distance cannot be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"electronics/coulombs_law.py","lineNumber":66,"sourceCode":"\n    >>> couloumbs_law(force=0, charge1=0, charge2=5, distance=2000)\n    Traceback (most recent call last):\n      ...\n    ValueError: One and only one argument must be 0\n\n    >>> couloumbs_law(force=0, charge1=3, charge2=5, distance=-2000)\n    Traceback (most recent call last):\n      ...\n    ValueError: Distance cannot be negative\n\n    \"\"\"\n\n    charge_product = abs(charge1 * charge2)\n\n    if (force, charge1, charge2, distance).count(0) != 1:\n        raise ValueError(\"One and only one argument must be 0\")\n    if distance < 0:\n        raise ValueError(\"Distance cannot be negative\")\n    if force == 0:\n        force = COULOMBS_CONSTANT * charge_product / (distance**2)\n        return {\"force\": force}\n    elif charge1 == 0:\n        charge1 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge2)\n        return {\"charge1\": charge1}\n    elif charge2 == 0:\n        charge2 = abs(force) * (distance**2) / (COULOMBS_CONSTANT * charge1)\n        return {\"charge2\": charge2}\n    elif distance == 0:\n        distance = (COULOMBS_CONSTANT * charge_product / abs(force)) ** 0.5\n        return {\"distance\": distance}\n    raise ValueError(\"Exactly one argument must be 0\")\n\n\nif __name__ == \"__main__\":\n    import doctest\n","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/electronics/coulombs_law.py#L48-L84","documentation":"Thrown by couloumbs_law() when distance < 0 while exactly one argument is 0. Distance in Coulomb's law is a separation magnitude (r^2 in the formula), so negative values are non-physical; only zero is tolerated — and only as the 'unknown' marker solved by sqrt(k*q1*q2/|F|).","triggerScenarios":"couloumbs_law(force=0, charge1=3, charge2=5, distance=-2000) — exactly one zero but a negative distance.","commonSituations":"Signed displacement vectors fed into a scalar law; coordinate differences computed as x1-x2 without abs(); physics engine integration reusing signed positions.","solutions":["Pass the magnitude of the separation: abs(x2 - x1).","Compute distance with math.dist / hypot which are always non-negative.","Reserve 0 exclusively for the argument you want solved."],"exampleFix":"# before\ncouloumbs_law(force=0, charge1=3, charge2=5, distance=x1 - x2)  # can be negative\n\n# after\ncouloumbs_law(force=0, charge1=3, charge2=5, distance=abs(x1 - x2))","handlingStrategy":"validation","validationCode":"from math import dist\nr = dist(p1, p2)  # always non-negative\nif r < 0:\n    raise AssertionError(\"unreachable\")\nresult = couloumbs_law(force=0, charge1=3, charge2=5, distance=r)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pass separation magnitude, never a signed coordinate difference.","Reserve distance=0 for 'solve for distance'."],"tags":["electronics","electrostatics","physics","validation","sign-error"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}