MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / object_distance

Function object_distance

physics/mirror_formulae.py:84–104  ·  view source on GitHub ↗

>>> 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): .

(focal_length: float, distance_of_image: float)

Source from the content-addressed store, hash-verified

82
83
84def object_distance(focal_length: float, distance_of_image: float) -> float:
85 """
86 >>> from math import isclose
87 >>> isclose(object_distance(30, 20), -60.0)
88 True
89 >>> from math import isclose
90 >>> isclose(object_distance(10.5, 11.7), 102.375)
91 True
92 >>> object_distance(90, 0) # doctest: +NORMALIZE_WHITESPACE
93 Traceback (most recent call last):
94 ...
95 ValueError: Invalid inputs. Enter non zero values with respect
96 to the sign convention.
97 """
98
99 if distance_of_image == 0 or focal_length == 0:
100 raise ValueError(
101 "Invalid inputs. Enter non zero values with respect to the sign convention."
102 )
103 object_distance = 1 / ((1 / focal_length) - (1 / distance_of_image))
104 return object_distance
105
106
107def image_distance(focal_length: float, distance_of_object: float) -> float:

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected