{"record":{"id":"b363bb0f84536153","repo":"TheAlgorithms/Python","slug":"the-value-of-intensity-cannot-be-negative","errorCode":null,"errorMessage":"The value of intensity cannot be negative","messagePattern":"The value of intensity cannot be negative","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/malus_law.py","lineNumber":69,"sourceCode":"    Traceback (most recent call last):\n        ...\n    ValueError: In Malus Law, the angle is in the range 0-360 degrees\n    >>> round(malus_law(10,900),2)\n    Traceback (most recent call last):\n        ...\n    ValueError: In Malus Law, the angle is in the range 0-360 degrees\n    >>> round(malus_law(-100,900),2)\n    Traceback (most recent call last):\n        ...\n    ValueError: The value of intensity cannot be negative\n    >>> round(malus_law(100,180),2)\n    100.0\n    >>> round(malus_law(100,360),2)\n    100.0\n    \"\"\"\n\n    if initial_intensity < 0:\n        raise ValueError(\"The value of intensity cannot be negative\")\n        # handling of negative values of initial intensity\n    if angle < 0 or angle > 360:\n        raise ValueError(\"In Malus Law, the angle is in the range 0-360 degrees\")\n        # handling of values out of allowed range\n    return initial_intensity * (math.cos(math.radians(angle)) ** 2)\n\n\nif __name__ == \"__main__\":\n    import doctest\n\n    doctest.testmod(name=\"malus_law\")\n","sourceCodeStart":51,"sourceCodeEnd":81,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/malus_law.py#L51-L81","documentation":"Raised by malus_law(initial_intensity, angle) when initial_intensity is negative. The function computes transmitted intensity I = I0 * cos^2(angle), which is only meaningful for a non-negative incident intensity. The angle check (0-360) happens after this one, so a call with both invalid raises this error first.","triggerScenarios":"malus_law(-100, 900) from the doctest — negative intensity checked before the out-of-range angle; any call with a signed intensity value from an instrument or subtraction.","commonSituations":"Detector offsets or dark-frame subtraction producing small negative readings; amplitude-like signed values confused with intensity; batch datasets with occasional negative samples.","solutions":["Clamp instrument readings with max(0, intensity) before calling.","Validate intensity >= 0 and 0 <= angle <= 360 in one pre-call check.","Catch ValueError when processing untrusted measurement data."],"exampleFix":"# before\nmalus_law(-100, 900)  # ValueError (intensity checked first)\n\n# after\nmalus_law(max(0, -100), min(900 % 360, 360))  # clamp/normalize first","handlingStrategy":"validation","validationCode":"intensity = max(0.0, measured_intensity)  # clamp sensor noise / dark-frame artifacts\nif 0 <= angle <= 360:\n    malus_law(intensity, angle)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["The intensity check runs first — fix both intensity and angle before retrying.","Clamp instrument readings to >= 0 after background subtraction.","Treat negative measured intensity as noise, not physics."],"tags":["physics","optics","polarization","input-validation","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}