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(distance_of_object, distance_of_image) in mirror_formulae.py when either distance is exactly 0. The mirror formula f = 1/(1/u + 1/v) needs both reciprocals, so zero is rejected to avoid division by zero. Negative distances are valid under the Cartesian sign convention used for mirrors.

Source

Thrown at physics/mirror_formulae.py:77


def focal_length(distance_of_object: float, distance_of_image: float) -> float:
    """
    >>> from math import isclose
    >>> isclose(focal_length(10, 20), 6.66666666666666)
    True
    >>> from math import isclose
    >>> isclose(focal_length(9.5, 6.7), 3.929012346)
    True
    >>> focal_length(0, 20)  # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Invalid inputs. Enter non zero values with respect
    to the sign convention.
    """

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


def object_distance(focal_length: float, distance_of_image: float) -> float:
    """
    >>> from math import isclose
    >>> isclose(object_distance(30, 20), -60.0)
    True
    >>> from math import isclose
    >>> isclose(object_distance(10.5, 11.7), 102.375)
    True
    >>> object_distance(90, 0)  # doctest: +NORMALIZE_WHITESPACE
    Traceback (most recent call last):
        ...
    ValueError: Invalid inputs. Enter non zero values with respect

View on GitHub (pinned to f5988cc097)

Solutions

  1. Ensure both distances are non-zero before calling (negatives allowed).
  2. Initialize optics parameters to non-zero sentinels or make them required.
  3. Catch ValueError to flag the degenerate on-mirror configuration.

Example fix

# before
focal_length(0, 20)  # ValueError

# after
focal_length(9.5, 6.7)  # 3.929012346
Defensive patterns

Strategy: validation

Validate before calling

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

if valid_mirror_distances(distance_of_object, distance_of_image):
    focal_length(distance_of_object, distance_of_image)

Try / catch

try:
    focal_length(u, v)
except ValueError:
    # object or image exactly at the mirror pole; handle degenerate ray here
    ...

Prevention

When it happens

Trigger: focal_length(0, 20) from the doctest; an object or image located exactly at the mirror pole.

Common situations: Zero-initialized ray-tracing parameters; geometry solvers converging to 0.0; sign-convention confusion leading to 0 instead of a small negative value.

Related errors


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