{"record":{"id":"c933dcbbb76561e1","repo":"TheAlgorithms/Python","slug":"mass-can-not-be-negative","errorCode":null,"errorMessage":"Mass can not be negative","messagePattern":"Mass can not be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/newtons_law_of_gravitation.py","lineNumber":79,"sourceCode":"        ...\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\nif __name__ == \"__main__\":\n    import doctest","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/newtons_law_of_gravitation.py#L61-L97","documentation":"Raised by gravitational_law() when mass_1 or mass_2 is negative. Mass is a physically non-negative quantity in this model, so either negative mass input is rejected after the count/force/distance checks. The single shared message does not say which mass is at fault.","triggerScenarios":"gravitational_law(force=1e12, mass_1=-674, mass_2=0, distance=9374); any call with mass_2 < 0 such as mass_2=-5.9e24; masses read from a file where a negative sign slipped in.","commonSituations":"Data-entry typos in mass tables; using signed placeholder values (e.g. -1 as 'unknown') instead of 0; propagating raw values from an import without sanitization.","solutions":["Remove the negative sign / use abs(mass) only if the sign is provably an error","If you used -1 or -1.0 as an 'unknown mass' sentinel, change it to 0 (the solver's actual sentinel) and ensure exactly one argument is 0","Log which mass is negative before calling so you can pinpoint the bad record in bulk pipelines"],"exampleFix":"# before\ngravitational_law(force=1e12, mass_1=-674, mass_2=0, distance=9374)\n# ValueError: Mass can not be negative\n\n# after\ngravitational_law(force=1e12, mass_1=674, mass_2=0, distance=9374)","handlingStrategy":"validation","validationCode":"if mass_1 < 0 or mass_2 < 0:\n    raise ValueError(f\"negative mass in record: mass_1={mass_1}, mass_2={mass_2}\")\ngravitational_law(force, mass_1, mass_2, distance)","typeGuard":null,"tryCatchPattern":"try:\n    gravitational_law(force, m1, m2, d)\nexcept ValueError as e:\n    if \"Mass\" in str(e):\n        log.bad_record(record_id)  # message does not say which mass\n    raise","preventionTips":["The shared message does not name the offending mass — log both values yourself","Never use negative sentinels for mass; the solver sentinel is 0, and only one 0 is allowed"],"tags":["physics","validation","negative-value","mass","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}