TheAlgorithms/Python · error · Exception

Temperature cannot be less than 0 K

Error message

Temperature cannot be less than 0 K

What it means

Raised by rms_speed_of_molecule(temperature, molar_mass) in physics/rms_speed_of_molecule.py when temperature < 0. The RMS speed v = sqrt(3*R*T/M) is undefined for negative absolute temperature. Note this module raises a bare built-in Exception, not ValueError, so 'except ValueError' will NOT catch it.

Source

Thrown at physics/rms_speed_of_molecule.py:34

have velocities of opposite signs. Since gas particles are in random motion, it's
plausible that there'll be about as several moving in one direction as within the other
way, which means that the average velocity for a collection of gas particles equals
zero; as this value is unhelpful, the average of velocities can be determined using an
alternative method.
"""

UNIVERSAL_GAS_CONSTANT = 8.3144598


def rms_speed_of_molecule(temperature: float, molar_mass: float) -> float:
    """
    >>> rms_speed_of_molecule(100, 2)
    35.315279554323226
    >>> rms_speed_of_molecule(273, 12)
    23.821458421977443
    """
    if temperature < 0:
        raise Exception("Temperature cannot be less than 0 K")
    if molar_mass <= 0:
        raise Exception("Molar mass cannot be less than or equal to 0 kg/mol")
    else:
        return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5


if __name__ == "__main__":
    import doctest

    # run doctest
    doctest.testmod()

    # example
    temperature = 300
    molar_mass = 28
    vrms = rms_speed_of_molecule(temperature, molar_mass)
    print(f"Vrms of Nitrogen gas at 300 K is {vrms} m/s")

View on GitHub (pinned to f5988cc097)

Solutions

  1. Convert Celsius to kelvin before calling: rms_speed_of_molecule(celsius + 273.15, molar_mass)
  2. Filter sensor sentinel values (e.g. -999) before computing
  3. Catch bare Exception (or Exception with message check) since this module does not raise ValueError
  4. Add a local pre-check temperature >= 0 to fail with your own clearer error

Example fix

# before
rms_speed_of_molecule(-50, 0.028)  # Celsius passed as kelvin
# Exception: Temperature cannot be less than 0 K

# after
rms_speed_of_molecule(-50 + 273.15, 0.028)
Defensive patterns

Strategy: try-catch

Validate before calling

if temperature < 0:
    temperature += 273.15  # if Celsius was passed; otherwise reject
if temperature < 0:
    raise ValueError(f"temperature must be >= 0 K, got {temperature}")
rms_speed_of_molecule(temperature, molar_mass)

Try / catch

try:
    v = rms_speed_of_molecule(temperature, molar_mass)
except Exception as e:  # NOT ValueError: module raises bare Exception
    if "Temperature" in str(e):
        raise ValueError("pass kelvin, not Celsius") from e
    raise

Prevention

When it happens

Trigger: rms_speed_of_molecule(-5, 2); temperatures read from sensors returning negative sentinel values; Celsius values passed where kelvin is required (e.g. -50 °C winter temperature passed directly).

Common situations: Unit confusion between Celsius and Kelvin (T_K = T_C + 273.15); ADC/sensor error codes like -999; simulation states with unphysical negative temperature after a numerical instability.

Related errors


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