{"record":{"id":"0a91f89415aafd8c","repo":"TheAlgorithms/Python","slug":"temperature-cannot-be-less-than-0-k","errorCode":null,"errorMessage":"Temperature cannot be less than 0 K","messagePattern":"Temperature cannot be less than 0 K","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"physics/rms_speed_of_molecule.py","lineNumber":34,"sourceCode":"have velocities of opposite signs. Since gas particles are in random motion, it's\nplausible that there'll be about as several moving in one direction as within the other\nway, which means that the average velocity for a collection of gas particles equals\nzero; as this value is unhelpful, the average of velocities can be determined using an\nalternative method.\n\"\"\"\n\nUNIVERSAL_GAS_CONSTANT = 8.3144598\n\n\ndef rms_speed_of_molecule(temperature: float, molar_mass: float) -> float:\n    \"\"\"\n    >>> rms_speed_of_molecule(100, 2)\n    35.315279554323226\n    >>> rms_speed_of_molecule(273, 12)\n    23.821458421977443\n    \"\"\"\n    if temperature < 0:\n        raise Exception(\"Temperature cannot be less than 0 K\")\n    if molar_mass <= 0:\n        raise Exception(\"Molar mass cannot be less than or equal to 0 kg/mol\")\n    else:\n        return (3 * UNIVERSAL_GAS_CONSTANT * temperature / molar_mass) ** 0.5\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    # run doctest\n    doctest.testmod()\n\n    # example\n    temperature = 300\n    molar_mass = 28\n    vrms = rms_speed_of_molecule(temperature, molar_mass)\n    print(f\"Vrms of Nitrogen gas at 300 K is {vrms} m/s\")\n","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/rms_speed_of_molecule.py#L16-L52","documentation":"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.","triggerScenarios":"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).","commonSituations":"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.","solutions":["Convert Celsius to kelvin before calling: rms_speed_of_molecule(celsius + 273.15, molar_mass)","Filter sensor sentinel values (e.g. -999) before computing","Catch bare Exception (or Exception with message check) since this module does not raise ValueError","Add a local pre-check temperature >= 0 to fail with your own clearer error"],"exampleFix":"# before\nrms_speed_of_molecule(-50, 0.028)  # Celsius passed as kelvin\n# Exception: Temperature cannot be less than 0 K\n\n# after\nrms_speed_of_molecule(-50 + 273.15, 0.028)","handlingStrategy":"try-catch","validationCode":"if temperature < 0:\n    temperature += 273.15  # if Celsius was passed; otherwise reject\nif temperature < 0:\n    raise ValueError(f\"temperature must be >= 0 K, got {temperature}\")\nrms_speed_of_molecule(temperature, molar_mass)","typeGuard":null,"tryCatchPattern":"try:\n    v = rms_speed_of_molecule(temperature, molar_mass)\nexcept Exception as e:  # NOT ValueError: module raises bare Exception\n    if \"Temperature\" in str(e):\n        raise ValueError(\"pass kelvin, not Celsius\") from e\n    raise","preventionTips":["Always convert Celsius to kelvin (T + 273.15) before calling","This module raises bare Exception, so except ValueError will not catch it","Filter sensor error codes like -999 before computing"],"tags":["physics","kinetic-theory","validation","unit-conversion","exception"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}