TheAlgorithms/Python · error · ValueError
Mass can not be negative
Error message
Mass can not be negative
What it means
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.
Source
Thrown at physics/newtons_law_of_gravitation.py:79
...
ValueError: Mass can not be negative
>>> gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374)
Traceback (most recent call last):
...
ValueError: Gravitational force can not be negative
"""
product_of_mass = mass_1 * mass_2
if (force, mass_1, mass_2, distance).count(0) != 1:
raise ValueError("One and only one argument must be 0")
if force < 0:
raise ValueError("Gravitational force can not be negative")
if distance < 0:
raise ValueError("Distance can not be negative")
if mass_1 < 0 or mass_2 < 0:
raise ValueError("Mass can not be negative")
if force == 0:
force = GRAVITATIONAL_CONSTANT * product_of_mass / (distance**2)
return {"force": force}
elif mass_1 == 0:
mass_1 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_2)
return {"mass_1": mass_1}
elif mass_2 == 0:
mass_2 = (force) * (distance**2) / (GRAVITATIONAL_CONSTANT * mass_1)
return {"mass_2": mass_2}
elif distance == 0:
distance = (GRAVITATIONAL_CONSTANT * product_of_mass / (force)) ** 0.5
return {"distance": distance}
raise ValueError("One and only one argument must be 0")
# Run doctest
if __name__ == "__main__":
import doctestView on GitHub (pinned to f5988cc097)
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
Example fix
# before gravitational_law(force=1e12, mass_1=-674, mass_2=0, distance=9374) # ValueError: Mass can not be negative # after gravitational_law(force=1e12, mass_1=674, mass_2=0, distance=9374)
Defensive patterns
Strategy: validation
Validate before calling
if mass_1 < 0 or mass_2 < 0:
raise ValueError(f"negative mass in record: mass_1={mass_1}, mass_2={mass_2}")
gravitational_law(force, mass_1, mass_2, distance) Try / catch
try:
gravitational_law(force, m1, m2, d)
except ValueError as e:
if "Mass" in str(e):
log.bad_record(record_id) # message does not say which mass
raise Prevention
- 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
When it happens
Trigger: 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.
Common situations: 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.
Related errors
- The mass of a body cannot be negative
- Gravitational force can not be negative
- Distance can not be negative
- The length should be non-negative
- The height above the ground cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/c933dcbbb76561e1.
Report an issue: GitHub.