TheAlgorithms/Python · error · ValueError
Area cannot be negative
Error message
Area cannot be negative
What it means
Raised by physics/shear_stress.py:shear_stress when the caller passes a negative value for the area argument. shear_stress solves for one of stress, tangential_force, or area from the other two, and a negative area is physically meaningless (it would flip the sign of the computed stress), so the function refuses it before doing any arithmetic. It is one of three per-argument sign checks that run after the 'exactly two of three inputs must be non-zero' check.
Source
Thrown at physics/shear_stress.py:38
2. Tangential Force
3. Cross-sectional Area
This is calculated from the other two provided values
Examples -
>>> shear_stress(stress=25, tangential_force=100, area=0)
('area', 4.0)
>>> shear_stress(stress=0, tangential_force=1600, area=200)
('stress', 8.0)
>>> shear_stress(stress=1000, tangential_force=0, area=1200)
('tangential_force', 1200000)
"""
if (stress, tangential_force, area).count(0) != 1:
raise ValueError("You cannot supply more or less than 2 values")
elif stress < 0:
raise ValueError("Stress cannot be negative")
elif tangential_force < 0:
raise ValueError("Tangential Force cannot be negative")
elif area < 0:
raise ValueError("Area cannot be negative")
elif stress == 0:
return (
"stress",
tangential_force / area,
)
elif tangential_force == 0:
return (
"tangential_force",
stress * area,
)
else:
return (
"area",
tangential_force / stress,
)
if __name__ == "__main__":View on GitHub (pinned to f5988cc097)
Solutions
- Pass a non-negative area; area must be > 0 when it is the unknown being solved for.
- Check argument order against the signature shear_stress(stress, tangential_force, area) - magnitudes differ by orders of magnitude so a swap is usually visible.
- If area is derived, clamp or validate the geometry inputs so the computed area cannot go negative.
- Wrap the call in try/except ValueError if negative intermediates are possible in your pipeline and you want to reject the sample explicitly.
Example fix
# before shear_stress(stress=0, tangential_force=1600, area=-200) # after shear_stress(stress=0, tangential_force=1600, area=200)
Defensive patterns
Strategy: validation
Validate before calling
if area < 0:
raise ValueError(f'area must be >= 0, got {area}')
result = shear_stress(stress=s, tangential_force=f, area=area) Type guard
def is_valid_shear_input(stress, tangential_force, area) -> bool:
return (stress, tangential_force, area).count(0) == 1 and min(stress, tangential_force, area) >= 0 Try / catch
try:
shear_stress(stress=s, tangential_force=f, area=a)
except ValueError as e:
if 'Area' in str(e):
# handle bad area specifically
... Prevention
- Use keyword arguments (stress=, tangential_force=, area=) to avoid positional swaps.
- Validate derived areas (differences of lengths) for sign before calling.
- Remember exactly one of the three inputs must be 0 to select the unknown.
When it happens
Trigger: Calling shear_stress with area < 0, e.g. shear_stress(stress=1000, tangential_force=500, area=-200) or shear_stress(stress=0, tangential_force=1600, area=-200). Note the check fires only when exactly one of the three arguments is 0; otherwise the 'more or less than 2 values' ValueError fires first.
Common situations: Swapping argument order so a force value lands in the area slot; computing area as a difference (a-b) that goes negative for degenerate geometry; porting unit conversions (cm^2 to m^2) that introduce a sign error; passing sentinel values like -1 for 'unknown'.
Related errors
- Gravitational force can not be negative
- Distance can not be negative
- Mass can not be negative
- Orbital radii must be greater than zero.
- The length should be non-negative
AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14).
Data as JSON: /api/errors/d6e4abc3c639331c.
Report an issue: GitHub.