TheAlgorithms/Python · error · ValueError

Invalid inputs. Enter non zero values with respect to the si

Error message

Invalid inputs. Enter non zero values with respect to the sign convention.

What it means

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.

Source

Thrown at physics/lens_formulae.py:70

    object_distance_from_lens: float, image_distance_from_lens: float
) -> float:
    """
    Doctests:
    >>> from math import isclose
    >>> isclose(focal_length_of_lens(10,4), 6.666666666666667)
    True
    >>> from math import isclose
    >>> isclose(focal_length_of_lens(2.7,5.8), -5.0516129032258075)
    True
    >>> focal_length_of_lens(0, 20)  # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Invalid inputs. Enter non zero values with respect
    to the sign convention.
    """

    if object_distance_from_lens == 0 or image_distance_from_lens == 0:
        raise ValueError(
            "Invalid inputs. Enter non zero values with respect to the sign convention."
        )
    focal_length = 1 / (
        (1 / image_distance_from_lens) - (1 / object_distance_from_lens)
    )
    return focal_length


def object_distance(
    focal_length_of_lens: float, image_distance_from_lens: float
) -> float:
    """
    Doctests:
    >>> from math import isclose
    >>> isclose(object_distance(10,40), -13.333333333333332)
    True

    >>> from math import isclose

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure both object and image distances are non-zero reals (negative allowed per sign convention).
  2. Replace 0-default placeholders with sentinel checks or required parameters before the call.
  3. Handle the object-on-lens (u=0) case in your own physics logic; the library deliberately refuses it.

Example fix

# before
focal_length_of_lens(0, 20)  # ValueError

# after
focal_length_of_lens(-20, 20)  # sign-convention values, non-zero
Defensive patterns

Strategy: validation

Validate before calling

def valid_lens_distances(*d: float) -> bool:
    return all(x != 0 for x in d)

if valid_lens_distances(object_distance, image_distance):
    focal_length_of_lens(object_distance, image_distance)

Try / catch

try:
    focal_length_of_lens(u, v)
except ValueError:
    # u or v was exactly 0; handle the on-lens degenerate case here
    ...

Prevention

When it happens

Trigger: focal_length_of_lens(0, 20) from the doctest; passing 0.0 because a default or an unset placeholder was never replaced.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Python@f5988cc097 (2026-08-14). Data as JSON: /api/errors/d83ac6b20f9ffe03. Report an issue: GitHub.