TheAlgorithms/Python · error · ValueError
Gravitational force can not be negative
Error message
Gravitational force can not be negative
What it means
Raised by gravitational_law() when the force argument is negative. Newtonian gravitational force magnitude is physically non-negative, so a negative input force is rejected before the solver branches run. The check fires even when force is the known value, not the 0 placeholder.
Source
Thrown at physics/newtons_law_of_gravitation.py:75
ValueError: One and only one argument must be 0
>>> gravitational_law(force=36337.283, mass_1=-674, mass_2=0, distance=35584)
Traceback (most recent call last):
...
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")
View on GitHub (pinned to f5988cc097)
Solutions
- Pass the magnitude: force=abs(force) if the sign only encoded direction
- If negative values indicate a repulsive/attractive convention upstream, convert to the magnitude before calling
- Sanitize imported datasets with force = abs(force) or drop rows with force < 0
Example fix
# before gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) # ValueError: Gravitational force can not be negative # after gravitational_law(force=abs(-847938e12), mass_1=674, mass_2=0, distance=9374)
Defensive patterns
Strategy: validation
Validate before calling
if force < 0:
force = abs(force) # only if sign encoded direction
# or reject:
assert force >= 0, "force must be a non-negative magnitude" Type guard
def is_non_negative(v) -> bool:
return isinstance(v, (int, float)) and v >= 0 Try / catch
try:
gravitational_law(force, m1, m2, d)
except ValueError as e:
if "force can not be negative" in str(e).lower():
gravitational_law(abs(force), m1, m2, d)
else:
raise Prevention
- Normalize signed physics-engine outputs to magnitudes before calling
- Sanitize imported datasets: drop or abs() negative force values explicitly
When it happens
Trigger: gravitational_law(force=-847938e12, mass_1=674, mass_2=0, distance=9374) — exactly one zero so the count check passes, but force < 0 raises immediately.
Common situations: Feeding signed vector components (attractive force coded as negative) into a magnitude-based API; parsing CSV data where a minus sign was a typo or a direction convention; reusing data from an orbital-mechanics library that signs its forces.
Related errors
- Distance can not be negative
- Mass can not be negative
- The length should be non-negative
- The mass of a body cannot be negative
- The height above the ground cannot be negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/756a934e2e2347ad.
Report an issue: GitHub.