{"record":{"id":"45d23576b4c71ad5","repo":"TheAlgorithms/Python","slug":"mass-can-t-be-negative","errorCode":null,"errorMessage":"Mass can't be negative.","messagePattern":"Mass can't be negative\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/mass_energy_equivalence.py","lineNumber":45,"sourceCode":"    in SI units J from Mass in kg.\n\n    mass (float): Mass of body.\n\n    Usage example:\n    >>> energy_from_mass(124.56)\n    1.11948945063458e+19\n    >>> energy_from_mass(320)\n    2.8760165719578165e+19\n    >>> energy_from_mass(0)\n    0.0\n    >>> energy_from_mass(-967.9)\n    Traceback (most recent call last):\n        ...\n    ValueError: Mass can't be negative.\n\n    \"\"\"\n    if mass < 0:\n        raise ValueError(\"Mass can't be negative.\")\n    return mass * c**2\n\n\ndef mass_from_energy(energy: float) -> float:\n    \"\"\"\n    Calculates the Mass equivalence of the Energy using m = E/c²\n    in SI units kg from Energy in J.\n\n    energy (float): Mass of body.\n\n    Usage example:\n    >>> mass_from_energy(124.56)\n    1.3859169098203872e-15\n    >>> mass_from_energy(320)\n    3.560480179371579e-15\n    >>> mass_from_energy(0)\n    0.0\n    >>> mass_from_energy(-967.9)","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/mass_energy_equivalence.py#L27-L63","documentation":"Raised by energy_from_mass(mass) when mass is negative; it computes E = m*c^2 with c = 299,792,458 m/s. Negative rest mass is unphysical for this Newtonian-to-relativistic conversion, so it is rejected. Zero mass is valid and returns 0.0.","triggerScenarios":"energy_from_mass(-967.9) from the doctest; any pipeline where a signed mass (e.g. mass defect with a sign convention) is passed directly.","commonSituations":"Mass-defect calculations in nuclear physics where the defect is sometimes reported negative; sign conventions in accounting-style mass balances; unit tests sweeping negative ranges.","solutions":["Pass mass as a non-negative magnitude; apply your own sign convention to the result if needed.","Pre-validate mass >= 0 for data-driven or user-supplied inputs.","Catch ValueError and re-raise with context about the offending value."],"exampleFix":"# before\nenergy_from_mass(-967.9)  # ValueError\n\n# after\nenergy_from_mass(967.9)   # 8.6908...e+19","handlingStrategy":"validation","validationCode":"if mass < 0:\n    raise ValueError(f'mass must be non-negative, got {mass}')\nenergy_from_mass(mass)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Apply sign conventions (e.g. mass defect) before the call; pass magnitudes.","mass == 0 is valid and returns 0.0.","Validate scientific inputs at parse/ingestion time."],"tags":["physics","relativity","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}