TheAlgorithms/Python · error · Exception

Absolute temperature cannot be less than 0 K

Error message

Absolute temperature cannot be less than 0 K

What it means

Raised by physics/speeds_of_gas_molecules.py:avg_speed_of_molecule when temperature < 0. The average molecular speed is sqrt(8RT/(pi*M)) and a negative absolute temperature would make the radicand negative. Note the library raises a bare Exception (not ValueError), so a caller filtering for ValueError will not catch it - you must catch Exception or check inputs beforehand.

Source

Thrown at physics/speeds_of_gas_molecules.py:76

    Examples:

    >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K
    454.3488755062257
    >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K
    445.5257273433045
    >>> avg_speed_of_molecule(-273, 0.028) # invalid temperature
    Traceback (most recent call last):
        ...
    Exception: Absolute temperature cannot be less than 0 K
    >>> avg_speed_of_molecule(273, 0) # invalid molar mass
    Traceback (most recent call last):
        ...
    Exception: Molar mass should be greater than 0 kg/mol
    """

    if temperature < 0:
        raise Exception("Absolute temperature cannot be less than 0 K")
    if molar_mass <= 0:
        raise Exception("Molar mass should be greater than 0 kg/mol")
    return (8 * R * temperature / (pi * molar_mass)) ** 0.5


def mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:
    """
    Takes the temperature (in K) and molar mass (in kg/mol) of a gas
    and returns the most probable speed of a molecule in the gas (in m/s).

    Examples:

    >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K
    402.65620702280023
    >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K
    394.8368955535605
    >>> mps_speed_of_molecule(-273, 0.028) # invalid temperature
    Traceback (most recent call last):

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert to kelvin before calling: T_K = T_C + 273.15 (or T_K = (T_F + 459.67) * 5/9).
  2. Sanity-check sensor/data feeds for negative kelvin values and reject or repair them upstream.
  3. Because this is a bare Exception, prefer pre-call validation over try/except; if you must catch it, use 'except Exception' and inspect the message.
  4. Upstream, the file could be improved to raise ValueError - file an issue or patch locally.

Example fix

# before (Celsius passed directly)
avg_speed_of_molecule(-40, 0.028)  # raises Exception

# after
temp_k = -40 + 273.15
avg_speed_of_molecule(temp_k, 0.028)
Defensive patterns

Strategy: validation

Validate before calling

temp_k = max(temp_c + 273.15, 0.0)
if temp_k < 0:
    raise ValueError('temperature below absolute zero')
v = avg_speed_of_molecule(temp_k, molar_mass)

Type guard

def is_valid_temperature_k(t: float) -> bool:
    return isinstance(t, (int, float)) and t >= 0

Try / catch

try:
    avg_speed_of_molecule(t, m)
except Exception as e:  # bare Exception, not ValueError
    if 'Absolute temperature' in str(e):
        ...

Prevention

When it happens

Trigger: Calling avg_speed_of_molecule(-273, 0.028) or with any negative first argument. Temperature must be in kelvin; passing a Celsius or Fahrenheit value that is negative (e.g. -40) triggers it even though the physical temperature is perfectly valid.

Common situations: Passing Celsius instead of kelvin - any Celsius temperature below 0 C triggers the guard; sensor data glitches producing negative spikes; sign error when computing a temperature difference T2-T1; copy-pasting a Fahrenheit value from a US-source dataset.

Related errors


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