{"record":{"id":"409a686c2c503d2a","repo":"TheAlgorithms/Python","slug":"distance-can-not-be-negative-409a68","errorCode":null,"errorMessage":"Distance can not be negative","messagePattern":"Distance can not be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/newtons_law_of_gravitation.py","lineNumber":77,"sourceCode":"    >>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584)\n    Traceback (most recent call last):\n        ...\n    ValueError: Mass can not be negative\n\n    >>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)\n    Traceback (most recent call last):\n        ...\n    ValueError: Gravitational force can not be negative\n    \"\"\"\n\n    product_of_mass = mass_1 * mass_2\n\n    if (force, mass_1, mass_2, distance).count(0) != 1:\n        raise ValueError(\"One and only one argument must be 0\")\n    if force < 0:\n        raise ValueError(\"Gravitational force can not be negative\")\n    if distance < 0:\n        raise ValueError(\"Distance can not be negative\")\n    if mass_1 < 0 or mass_2 < 0:\n        raise ValueError(\"Mass can not be negative\")\n    if force == 0:\n        force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)\n        return {\"force\": force}\n    elif mass_1 == 0:\n        mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2)\n        return {\"mass_1\": mass_1}\n    elif mass_2 == 0:\n        mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1)\n        return {\"mass_2\": mass_2}\n    elif distance == 0:\n        distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5\n        return {\"distance\": distance}\n    raise ValueError(\"One and only one argument must be 0\")\n\n\n# Run doctest","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/newtons_law_of_gravitation.py#L59-L95","documentation":"Raised by gravitational_law() when distance is negative. Separation between two masses is a non-negative scalar in the inverse-square law, so negative distances are rejected after the zero-count and force checks. It fires whether distance is the known value or would be used in a computation.","triggerScenarios":"gravitational_law(force=847938e12, mass_1=674, mass_2=0, distance=-9374); any call where the fourth argument is less than 0, e.g. distance computed as (a - b) that underflowed to a negative number.","commonSituations":"Coordinate-frame mistakes where a relative position along one axis is used as a distance; subtraction that produced a negative value due to argument order (distance = p1 - p2 instead of abs(p1 - p2)); unit tests with arbitrary sign-filled fixtures.","solutions":["Compute distance as a magnitude, e.g. distance = abs(p2 - p1) or math.dist(pos1, pos2)","Fix argument order in the subtraction producing the distance","Validate distance >= 0 at the data source before feeding the solver"],"exampleFix":"# before\ndistance = body2_x - body1_x  # can be negative\ngravitational_law(force=1e12, mass_1=674, mass_2=0, distance=distance)\n\n# after\ndistance = abs(body2_x - body1_x)\ngravitational_law(force=1e12, mass_1=674, mass_2=0, distance=distance)","handlingStrategy":"validation","validationCode":"import math\ndistance = math.dist(position_1, position_2)  # always >= 0\nassert distance >= 0","typeGuard":null,"tryCatchPattern":"try:\n    gravitational_law(force, m1, m2, distance)\nexcept ValueError as e:\n    if \"Distance\" in str(e):\n        raise ValueError(f\"bad separation: {distance}\") from e\n    raise","preventionTips":["Compute distances with math.dist or abs(a - b), never raw subtraction","Check for argument-order mistakes when distances come out negative"],"tags":["physics","validation","negative-value","distance","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}