TheAlgorithms/Python · error · ValueError

mass, density, area and the drag coefficient all need to be

Error message

mass, density, area and the drag coefficient all need to be positive

What it means

Raised by physics/terminal_velocity.py:terminal_velocity when any of mass, density, area, or drag_coefficient is <= 0. The formula sqrt(2*m*g/(rho*A*Cd)) requires all four to be strictly positive; the check is a single combined guard, so the message does not tell you which parameter failed. Inputs are positional, which makes silent argument swaps the most common cause.

Source

Thrown at physics/terminal_velocity.py:51

    >>> terminal_velocity(2, 100, 0.45, 0.23)
    1.9467947148674276
    >>> terminal_velocity(5, 50, 0.2, 0.5)
    4.428690551393267
    >>> terminal_velocity(-5, 50, -0.2, -2)
    Traceback (most recent call last):
        ...
    ValueError: mass, density, area and the drag coefficient all need to be positive
    >>> terminal_velocity(3, -20, -1, 2)
    Traceback (most recent call last):
        ...
    ValueError: mass, density, area and the drag coefficient all need to be positive
    >>> terminal_velocity(-2, -1, -0.44, -1)
    Traceback (most recent call last):
        ...
    ValueError: mass, density, area and the drag coefficient all need to be positive
    """
    if mass <= 0 or density <= 0 or area <= 0 or drag_coefficient <= 0:
        raise ValueError(
            "mass, density, area and the drag coefficient all need to be positive"
        )
    return ((2 * mass * g) / (density * area * drag_coefficient)) ** 0.5


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Check each of the four values individually before the call to find which one is non-positive (the error message does not identify it).
  2. Call with keyword arguments - terminal_velocity(mass=70, density=1.2, area=0.7, drag_coefficient=1.0) - to eliminate positional swaps.
  3. Fix defaults: use realistic fallbacks like density=1.225 (sea-level air) instead of 0 or -1 sentinels.
  4. If invalid parameters occur in batch simulations, catch ValueError and skip/quarantine that record.

Example fix

# before (1.2 meant as density lands in mass)
terminal_velocity(1.2, 70, 0.44, 1.0)

# after
terminal_velocity(mass=70, density=1.2, area=0.44, drag_coefficient=1.0)
Defensive patterns

Strategy: validation

Validate before calling

params = {'mass': m, 'density': rho, 'area': A, 'drag_coefficient': cd}
bad = [k for k, v in params.items() if v <= 0]
if bad:
    raise ValueError(f'non-positive parameters: {bad}')
vt = terminal_velocity(m, rho, A, cd)

Type guard

def is_valid_terminal_velocity_args(m, rho, a, cd) -> bool:
    return all(isinstance(x, (int, float)) and x > 0 for x in (m, rho, a, cd))

Try / catch

try:
    terminal_velocity(m, rho, A, cd)
except ValueError:
    # message does not say which param failed; re-check all four here
    ...

Prevention

When it happens

Trigger: Calling terminal_velocity with any non-positive argument, e.g. terminal_velocity(3, -20, 0.44, 1) (negative density), terminal_velocity(0, 1.2, 0.44, 1) (zero mass), or several at once as in the doctest terminal_velocity(-2, -1, -0.44, -1).

Common situations: Positional-argument swaps - the signature is (mass, density, area, drag_coefficient) and passing 1.2 (air density) as mass or 70 (mass) as density are both classic mistakes; zero drag coefficient from an unconfigured aero model; density defaults of 0 when the medium is unspecified; negative values from upstream subtraction of sensor offsets.

Related errors


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