TheAlgorithms/Python · error · ValueError

Stress cannot be negative

Error message

Stress cannot be negative

What it means

Raised by shear_stress() when the stress argument is negative (and exactly one argument is 0). Stress in this model is a non-negative magnitude, so a negative value is rejected before the solve branches. Note the elif chain order: a negative stress masks negative force/area errors.

Source

Thrown at physics/shear_stress.py:34

) -> tuple[str, float]:
    """
    This function can calculate any one of the three -
    1. Shear Stress
    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,

View on GitHub (pinned to f5988cc097)

Solutions

  1. Pass the magnitude: shear_stress(stress=abs(sigma), ...)
  2. Convert from your FEA convention first, e.g. take abs() of signed principal stress
  3. Sanitize imported data by rejecting or abs()-ing negative stress values deliberately, with a log line

Example fix

# before
shear_stress(stress=-25, tangential_force=100, area=0)
# ValueError: Stress cannot be negative

# after
shear_stress(stress=abs(-25), tangential_force=100, area=0)
Defensive patterns

Strategy: validation

Validate before calling

if stress < 0:
    stress = abs(stress)  # convert FEA signed stress to magnitude
shear_stress(stress, tangential_force, area)

Try / catch

try:
    shear_stress(stress, tangential_force, area)
except ValueError as e:
    if "Stress cannot be negative" in str(e):
        shear_stress(abs(stress), tangential_force, area)
    else:
        raise

Prevention

When it happens

Trigger: shear_stress(stress=-25, tangential_force=100, area=0) — negative stress with area as the unknown; signed stress from a FEA solver where compression is negative fed directly.

Common situations: FEA continuum-mechanics sign conventions (compressive stress negative) not converted to magnitudes; CSV sign errors; reusing values from a Mohr's-circle computation that returned signed principal stresses.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/2ce3d8a78e41850c. Report an issue: GitHub.