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

Function focal_length_of_lens

physics/lens_formulae.py:51–76  ·  view source on GitHub ↗

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

(
    object_distance_from_lens: float, image_distance_from_lens: float
)

Source from the content-addressed store, hash-verified

49
50
51def focal_length_of_lens(
52 object_distance_from_lens: float, image_distance_from_lens: float
53) -> float:
54 """
55 Doctests:
56 >>> from math import isclose
57 >>> isclose(focal_length_of_lens(10,4), 6.666666666666667)
58 True
59 >>> from math import isclose
60 >>> isclose(focal_length_of_lens(2.7,5.8), -5.0516129032258075)
61 True
62 >>> focal_length_of_lens(0, 20) # doctest: +NORMALIZE_WHITESPACE
63 Traceback (most recent call last):
64 ...
65 ValueError: Invalid inputs. Enter non zero values with respect
66 to the sign convention.
67 """
68
69 if object_distance_from_lens == 0 or image_distance_from_lens == 0:
70 raise ValueError(
71 "Invalid inputs. Enter non zero values with respect to the sign convention."
72 )
73 focal_length = 1 / (
74 (1 / image_distance_from_lens) - (1 / object_distance_from_lens)
75 )
76 return focal_length
77
78
79def object_distance(

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected