{"record":{"id":"86915b037449b574","repo":"TheAlgorithms/Python","slug":"the-mass-of-a-body-cannot-be-negative-86915b","errorCode":null,"errorMessage":"The mass of a body cannot be negative","messagePattern":"The mass of a body cannot be negative","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/potential_energy.py","lineNumber":51,"sourceCode":"    \"\"\"\n    >>> potential_energy(10,10)\n    980.665\n    >>> potential_energy(0,5)\n    0.0\n    >>> potential_energy(8,0)\n    0.0\n    >>> potential_energy(10,5)\n    490.3325\n    >>> potential_energy(0,0)\n    0.0\n    >>> potential_energy(2,8)\n    156.9064\n    >>> potential_energy(20,100)\n    19613.3\n    \"\"\"\n    if mass < 0:\n        # handling of negative values of mass\n        raise ValueError(\"The mass of a body cannot be negative\")\n    if height < 0:\n        # handling of negative values of height\n        raise ValueError(\"The height above the ground cannot be negative\")\n    return mass * g * height\n\n\nif __name__ == \"__main__\":\n    from doctest import testmod\n\n    testmod(name=\"potential_energy\")\n","sourceCodeStart":33,"sourceCodeEnd":62,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/potential_energy.py#L33-L62","documentation":"Raised by potential_energy(mass, height) in physics/potential_energy.py when mass < 0. Gravitational potential energy U = m*g*h requires a non-negative mass; mass 0 is allowed and returns 0.0. The mass check runs first, so a negative mass raises even if height is also invalid.","triggerScenarios":"potential_energy(-10, 5); any call with a negative first argument, e.g. mass loaded from a physics body whose serialization used -1 for unknown mass.","commonSituations":"Using sentinel values like -1 for missing mass; importing bodies from game engines where negative mass means 'static/infinite'; sign errors from mass deltas (rocket propellant consumed) fed as absolute mass.","solutions":["Pass a non-negative mass in kg: potential_energy(10, 5) -> 490.3325","Replace sentinel masses (e.g. -1) with a validation step that rejects the record before calling","If computing deltas, compute potential_energy on absolute masses and subtract the results instead of passing a negative delta mass"],"exampleFix":"# before\npotential_energy(-10, 5)\n# ValueError: The mass of a body cannot be negative\n\n# after\npotential_energy(10, 5)","handlingStrategy":"validation","validationCode":"if mass < 0:\n    raise ValueError(f\"mass must be >= 0, got {mass}\")\npotential_energy(mass, height)","typeGuard":null,"tryCatchPattern":"try:\n    potential_energy(mass, height)\nexcept ValueError as e:\n    if \"mass\" in str(e):\n        raise ValueError(f\"bad mass in payload: {mass}\") from e\n    raise","preventionTips":["Mass check runs before height check — fix mass first if both are invalid","Replace -1/None mass sentinels with explicit rejection upstream"],"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"}