{"record":{"id":"ed3047676e7f3fb2","repo":"TheAlgorithms/Python","slug":"frequency-can-t-be-negative","errorCode":null,"errorMessage":"Frequency can't be negative.","messagePattern":"Frequency can't be negative\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/photoelectric_effect.py","lineNumber":58,"sourceCode":"    >>> maximum_kinetic_energy(1000000,2)\r\n    0\r\n    >>> maximum_kinetic_energy(1000000,2,True)\r\n    0\r\n    >>> maximum_kinetic_energy(10000000000000000,2,True)\r\n    39.357000000000006\r\n    >>> maximum_kinetic_energy(-9,20)\r\n    Traceback (most recent call last):\r\n        ...\r\n    ValueError: Frequency can't be negative.\r\n\r\n    >>> maximum_kinetic_energy(1000,\"a\")\r\n    Traceback (most recent call last):\r\n        ...\r\n    TypeError: unsupported operand type(s) for -: 'float' and 'str'\r\n\r\n    \"\"\"\r\n    if frequency < 0:\r\n        raise ValueError(\"Frequency can't be negative.\")\r\n    if in_ev:\r\n        return max(PLANCK_CONSTANT_EVS * frequency - work_function, 0)\r\n    return max(PLANCK_CONSTANT_JS * frequency - work_function, 0)\r\n\r\n\r\nif __name__ == \"__main__\":\r\n    import doctest\r\n\r\n    doctest.testmod()\r\n","sourceCodeStart":40,"sourceCodeEnd":68,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/photoelectric_effect.py#L40-L68","documentation":"Raised by maximum_kinetic_energy(frequency, work_function, in_ev) in physics/photoelectric_effect.py when frequency < 0. The photoelectric equation K_max = h*f - W needs a non-negative frequency; negative frequencies are unphysical and rejected before the max() clamp runs. Note the sibling doctest shows non-numeric work_function raises TypeError from the subtraction, not a dedicated message.","triggerScenarios":"maximum_kinetic_energy(-9, 20) — negative frequency as in the doctest; frequencies derived from wavelength via f = c/wavelength where a negative wavelength was passed in; data arrays containing sentinel values like -1.","commonSituations":"Unit confusion between wavelength and frequency passed into the frequency slot; spectroscopy datasets using -1 as a missing-value marker; sign conventions from Doppler-shift code (blueshift/redshift as signed deltas) reused directly.","solutions":["Pass a non-negative frequency in Hz, e.g. maximum_kinetic_energy(1e15, 2.0)","If your data is wavelengths, convert first: frequency = 299792458 / wavelength, and validate wavelength > 0","Filter sentinel values (frequency < 0) out of datasets before computing"],"exampleFix":"# before\nmaximum_kinetic_energy(-9, 20)\n# ValueError: Frequency can't be negative.\n\n# after\nfrequency = 299792458 / wavelength  # wavelength must be > 0\nmaximum_kinetic_energy(frequency, 20)","handlingStrategy":"validation","validationCode":"if frequency < 0:\n    raise ValueError(f\"frequency must be >= 0 Hz, got {frequency}\")\nmaximum_kinetic_energy(frequency, work_function)","typeGuard":null,"tryCatchPattern":"try:\n    ke = maximum_kinetic_energy(frequency, work_function)\nexcept ValueError as e:\n    if \"Frequency\" in str(e):\n        raise ValueError(\"check wavelength->frequency conversion\") from e\n    raise","preventionTips":["Convert wavelength to frequency (f = c/lambda, lambda > 0) before calling","Strip -1 'missing value' sentinels from spectroscopy datasets"],"tags":["physics","photoelectric-effect","validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}