{"record":{"id":"756a934e2e2347ad","repo":"TheAlgorithms/Python","slug":"gravitational-force-can-not-be-negative","errorCode":null,"errorMessage":"Gravitational force can not be negative","messagePattern":"Gravitational force can not be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/newtons_law_of_gravitation.py","lineNumber":75,"sourceCode":"    ValueError: One and only one argument must be 0\n\n    >>> 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","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/newtons_law_of_gravitation.py#L57-L93","documentation":"Raised by gravitational_law() when the force argument is negative. Newtonian gravitational force magnitude is physically non-negative, so a negative input force is rejected before the solver branches run. The check fires even when force is the known value, not the 0 placeholder.","triggerScenarios":"gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) — exactly one zero so the count check passes, but force < 0 raises immediately.","commonSituations":"Feeding signed vector components (attractive force coded as negative) into a magnitude-based API; parsing CSV data where a minus sign was a typo or a direction convention; reusing data from an orbital-mechanics library that signs its forces.","solutions":["Pass the magnitude: force=abs(force) if the sign only encoded direction","If negative values indicate a repulsive/attractive convention upstream, convert to the magnitude before calling","Sanitize imported datasets with force = abs(force) or drop rows with force < 0"],"exampleFix":"# before\ngravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)\n# ValueError: Gravitational force can not be negative\n\n# after\ngravitational_law(force=abs(-847938e12), mass_1=674, mass_2=0, distance=9374)","handlingStrategy":"validation","validationCode":"if force < 0:\n    force = abs(force)  # only if sign encoded direction\n# or reject:\nassert force >= 0, \"force must be a non-negative magnitude\"","typeGuard":"def is_non_negative(v) -> bool:\n    return isinstance(v, (int, float)) and v >= 0","tryCatchPattern":"try:\n    gravitational_law(force, m1, m2, d)\nexcept ValueError as e:\n    if \"force can not be negative\" in str(e).lower():\n        gravitational_law(abs(force), m1, m2, d)\n    else:\n        raise","preventionTips":["Normalize signed physics-engine outputs to magnitudes before calling","Sanitize imported datasets: drop or abs() negative force values explicitly"],"tags":["physics","validation","negative-value","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}