{"record":{"id":"53669b87c1322816","repo":"TheAlgorithms/Python","slug":"absolute-temperature-cannot-be-less-than-0-k","errorCode":null,"errorMessage":"Absolute temperature cannot be less than 0 K","messagePattern":"Absolute temperature cannot be less than 0 K","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"physics/speeds_of_gas_molecules.py","lineNumber":76,"sourceCode":"\n    Examples:\n\n    >>> avg_speed_of_molecule(273, 0.028) # nitrogen at 273 K\n    454.3488755062257\n    >>> avg_speed_of_molecule(300, 0.032) # oxygen at 300 K\n    445.5257273433045\n    >>> avg_speed_of_molecule(-273, 0.028) # invalid temperature\n    Traceback (most recent call last):\n        ...\n    Exception: Absolute temperature cannot be less than 0 K\n    >>> avg_speed_of_molecule(273, 0) # invalid molar mass\n    Traceback (most recent call last):\n        ...\n    Exception: Molar mass should be greater than 0 kg/mol\n    \"\"\"\n\n    if temperature < 0:\n        raise Exception(\"Absolute temperature cannot be less than 0 K\")\n    if molar_mass <= 0:\n        raise Exception(\"Molar mass should be greater than 0 kg/mol\")\n    return (8 * R * temperature / (pi * molar_mass)) ** 0.5\n\n\ndef mps_speed_of_molecule(temperature: float, molar_mass: float) -> float:\n    \"\"\"\n    Takes the temperature (in K) and molar mass (in kg/mol) of a gas\n    and returns the most probable speed of a molecule in the gas (in m/s).\n\n    Examples:\n\n    >>> mps_speed_of_molecule(273, 0.028) # nitrogen at 273 K\n    402.65620702280023\n    >>> mps_speed_of_molecule(300, 0.032) # oxygen at 300 K\n    394.8368955535605\n    >>> mps_speed_of_molecule(-273, 0.028) # invalid temperature\n    Traceback (most recent call last):","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/speeds_of_gas_molecules.py#L58-L94","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert to kelvin before calling: T_K = T_C + 273.15 (or T_K = (T_F + 459.67) * 5/9).","Sanity-check sensor/data feeds for negative kelvin values and reject or repair them upstream.","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.","Upstream, the file could be improved to raise ValueError - file an issue or patch locally."],"exampleFix":"# before (Celsius passed directly)\navg_speed_of_molecule(-40, 0.028)  # raises Exception\n\n# after\ntemp_k = -40 + 273.15\navg_speed_of_molecule(temp_k, 0.028)","handlingStrategy":"validation","validationCode":"temp_k = max(temp_c + 273.15, 0.0)\nif temp_k < 0:\n    raise ValueError('temperature below absolute zero')\nv = avg_speed_of_molecule(temp_k, molar_mass)","typeGuard":"def is_valid_temperature_k(t: float) -> bool:\n    return isinstance(t, (int, float)) and t >= 0","tryCatchPattern":"try:\n    avg_speed_of_molecule(t, m)\nexcept Exception as e:  # bare Exception, not ValueError\n    if 'Absolute temperature' in str(e):\n        ...","preventionTips":["Convert Celsius/Fahrenheit to kelvin at the system boundary, once.","Do not rely on 'except ValueError' - this module raises bare Exception.","Sanitize sensor feeds for negative-kelvin glitches before physics calls."],"tags":["physics","gas","units","temperature","bare-exception"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}