{"record":{"id":"d83ac6b20f9ffe03","repo":"TheAlgorithms/Python","slug":"invalid-inputs-enter-non-zero-values-with-respect","errorCode":null,"errorMessage":"Invalid inputs. Enter non zero values with respect to the sign convention.","messagePattern":"Invalid inputs\\. Enter non zero values with respect to the sign convention\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"physics/lens_formulae.py","lineNumber":70,"sourceCode":"    object_distance_from_lens: float, image_distance_from_lens: float\n) -> float:\n    \"\"\"\n    Doctests:\n    >>> from math import isclose\n    >>> isclose(focal_length_of_lens(10,4), 6.666666666666667)\n    True\n    >>> from math import isclose\n    >>> isclose(focal_length_of_lens(2.7,5.8), -5.0516129032258075)\n    True\n    >>> focal_length_of_lens(0, 20)  # doctest: +NORMALIZE_WHITESPACE\n    Traceback (most recent call last):\n        ...\n    ValueError: Invalid inputs. Enter non zero values with respect\n    to the sign convention.\n    \"\"\"\n\n    if object_distance_from_lens == 0 or image_distance_from_lens == 0:\n        raise ValueError(\n            \"Invalid inputs. Enter non zero values with respect to the sign convention.\"\n        )\n    focal_length = 1 / (\n        (1 / image_distance_from_lens) - (1 / object_distance_from_lens)\n    )\n    return focal_length\n\n\ndef object_distance(\n    focal_length_of_lens: float, image_distance_from_lens: float\n) -> float:\n    \"\"\"\n    Doctests:\n    >>> from math import isclose\n    >>> isclose(object_distance(10,40), -13.333333333333332)\n    True\n\n    >>> from math import isclose","sourceCodeStart":52,"sourceCodeEnd":88,"githubUrl":"https://github.com/TheAlgorithms/Python/blob/f5988cc09713315817df6a7e327e258013a94440/physics/lens_formulae.py#L52-L88","documentation":"Raised by focal_length_of_lens(object_distance_from_lens, image_distance_from_lens) when either distance is exactly 0. The lens formula computes f = 1/(1/v - 1/u), so a zero distance would divide by zero; the guard converts that into a clear ValueError. Negative distances ARE valid here (Cartesian sign convention) — only zero is rejected.","triggerScenarios":"focal_length_of_lens(0, 20) from the doctest; passing 0.0 because a default or an unset placeholder was never replaced.","commonSituations":"Default-initializing distances to 0 in config dicts; upstream geometry returning 0 when an object sits exactly on the lens; forgetting that this library uses the sign convention where 0 is the degenerate case.","solutions":["Ensure both object and image distances are non-zero reals (negative allowed per sign convention).","Replace 0-default placeholders with sentinel checks or required parameters before the call.","Handle the object-on-lens (u=0) case in your own physics logic; the library deliberately refuses it."],"exampleFix":"# before\nfocal_length_of_lens(0, 20)  # ValueError\n\n# after\nfocal_length_of_lens(-20, 20)  # sign-convention values, non-zero","handlingStrategy":"validation","validationCode":"def valid_lens_distances(*d: float) -> bool:\n    return all(x != 0 for x in d)\n\nif valid_lens_distances(object_distance, image_distance):\n    focal_length_of_lens(object_distance, image_distance)","typeGuard":null,"tryCatchPattern":"try:\n    focal_length_of_lens(u, v)\nexcept ValueError:\n    # u or v was exactly 0; handle the on-lens degenerate case here\n    ...","preventionTips":["Zero is the only forbidden value; negatives are meaningful (sign convention).","Never default lens distances to 0 in configs or dataclasses.","Handle object-on-lens (u=0) as an explicit special case in your optics model."],"tags":["physics","optics","lens","zero-check","valueerror"],"backgroundTag":null,"analyzedSha":"f5988cc09713315817df6a7e327e258013a94440","analyzedAt":"2026-08-14T17:30:07.041Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}