TheAlgorithms/Python · error · ValueError

Atmospheric Pressure can not be negative !

Error message

Atmospheric Pressure can not be negative !

What it means

Raised by get_altitude_at_pressure when the supplied pressure is negative. Atmospheric pressure is physically non-negative, and the fractional power in the barometric formula would be undefined for negative operands, so the guard runs after the sea-level-maximum check.

Source

Thrown at physics/altitude_pressure.py:45

    105.47836610778828
    >>> get_altitude_at_pressure(pressure=101_325)
    0.0
    >>> get_altitude_at_pressure(pressure=80_000)
    1855.873388064995
    >>> get_altitude_at_pressure(pressure=201_325)
    Traceback (most recent call last):
      ...
    ValueError: Value Higher than Pressure at Sea Level !
    >>> get_altitude_at_pressure(pressure=-80_000)
    Traceback (most recent call last):
      ...
    ValueError: Atmospheric Pressure can not be negative !
    """

    if pressure > 101325:
        raise ValueError("Value Higher than Pressure at Sea Level !")
    if pressure < 0:
        raise ValueError("Atmospheric Pressure can not be negative !")
    return 44_330 * (1 - (pressure / 101_325) ** (1 / 5.5255))


if __name__ == "__main__":
    import doctest

    doctest.testmod()

View on GitHub (pinned to f5988cc097)

Solutions

  1. Validate sensor/config data and reject or replace negative pressure readings before the call
  2. Check for sentinel values (e.g. -1, -9999) that instruments use to signal errors

Example fix

# before
get_altitude_at_pressure(pressure=sensor_reading)  # -80000 -> ValueError

# after
if sensor_reading is not None and sensor_reading >= 0:
    altitude = get_altitude_at_pressure(pressure=sensor_reading)
Defensive patterns

Strategy: validation

Validate before calling

def plausible_pressure(pressure_pa) -> bool:
    return pressure_pa >= 0

Prevention

When it happens

Trigger: Calling get_altitude_at_pressure(pressure=-80_000) or passing any negative pressure value, e.g. sensor readings that encode error states as negative numbers.

Common situations: Uninitialized or faulting pressure sensors reporting negative sentinel values, or signed-unit mistakes where a converted value flips sign.

Related errors


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