{"record":{"id":"e6a3d58ab897df3e","repo":"TheAlgorithms/Python","slug":"the-mass-of-a-body-cannot-be-negative","errorCode":null,"errorMessage":"The mass of a body cannot be negative","messagePattern":"The mass of a body cannot be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/kinetic_energy.py","lineNumber":43,"sourceCode":"    The kinetic energy of a non-rotating object of mass m traveling at a speed v is ½mv²\n\n    >>> kinetic_energy(10,10)\n    500.0\n    >>> kinetic_energy(0,10)\n    0.0\n    >>> kinetic_energy(10,0)\n    0.0\n    >>> kinetic_energy(20,-20)\n    4000.0\n    >>> kinetic_energy(0,0)\n    0.0\n    >>> kinetic_energy(2,2)\n    4.0\n    >>> kinetic_energy(100,100)\n    500000.0\n    \"\"\"\n    if mass < 0:\n        raise ValueError(\"The mass of a body cannot be negative\")\n    return 0.5 * mass * abs(velocity) * abs(velocity)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod(verbose=True)\n","sourceCodeStart":25,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/kinetic_energy.py#L25-L51","documentation":"Raised by kinetic_energy(mass, velocity) when mass is negative; kinetic energy KE = 0.5*m*|v|^2 requires non-negative mass. Velocity sign is intentionally allowed — the function applies abs(velocity), and the doctest shows kinetic_energy(20, -20) == 4000.0.","triggerScenarios":"kinetic_energy(-5, 10); passing a signed coordinate or momentum-like value where mass is expected. Negative velocity never triggers this error.","commonSituations":"Vector components with signs fed into a scalar function; mass values from a solver iteration that went negative; confusing the parameter order and passing velocity first.","solutions":["Pass mass as a non-negative magnitude; direction of velocity does not matter here.","Add a pre-call check `if mass < 0: ...` for data-driven pipelines.","If a negative result from physics computation appears, fix the upstream state, not this guard."],"exampleFix":"# before\nkinetic_energy(-10, 0)  # ValueError\n\n# after\nkinetic_energy(10, 0)   # 0.0; velocity may be any sign","handlingStrategy":"validation","validationCode":"if mass < 0:\n    raise ValueError(f'mass must be non-negative, got {mass}')\nkinetic_energy(mass, velocity)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Only mass is sign-checked; any velocity sign is fine because abs() is applied.","Check argument order — (mass, velocity), not (velocity, mass).","For vector inputs, pass the mass scalar and the velocity magnitude or signed component as suits your formula."],"tags":["physics","mechanics","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}