MCPcopy Index your code
hub / github.com/python/cpython / result_check

Function result_check

Lib/test/test_math.py:126–172  ·  view source on GitHub ↗

Compare arguments expected and got, as floats, if either is a float, using a tolerance expressed in multiples of ulp(expected) or absolutely (if given and greater). As a convenience, when neither argument is a float, and for non-finite floats, exact equality is demanded. Also, nan==

(expected, got, ulp_tol=5, abs_tol=0.0)

Source from the content-addressed store, hash-verified

124
125
126def result_check(expected, got, ulp_tol=5, abs_tol=0.0):
127 # Common logic of MathTests.(ftest, test_testcases, test_mtestcases)
128 """Compare arguments expected and got, as floats, if either
129 is a float, using a tolerance expressed in multiples of
130 ulp(expected) or absolutely (if given and greater).
131
132 As a convenience, when neither argument is a float, and for
133 non-finite floats, exact equality is demanded. Also, nan==nan
134 as far as this function is concerned.
135
136 Returns None on success and an error message on failure.
137 """
138
139 # Check exactly equal (applies also to strings representing exceptions)
140 if got == expected:
141 if not got and not expected:
142 if math.copysign(1, got) != math.copysign(1, expected):
143 return f"expected {expected}, got {got} (zero has wrong sign)"
144 return None
145
146 failure = "not equal"
147
148 # Turn mixed float and int comparison (e.g. floor()) to all-float
149 if isinstance(expected, float) and isinstance(got, int):
150 got = float(got)
151 elif isinstance(got, float) and isinstance(expected, int):
152 expected = float(expected)
153
154 if isinstance(expected, float) and isinstance(got, float):
155 if math.isnan(expected) and math.isnan(got):
156 # Pass, since both nan
157 failure = None
158 elif math.isinf(expected) or math.isinf(got):
159 # We already know they're not equal, drop through to failure
160 pass
161 else:
162 # Both are finite floats (now). Are they close enough?
163 failure = ulp_abs_check(expected, got, ulp_tol, abs_tol)
164
165 # arguments are not equal, and if numeric, are too far apart
166 if failure is not None:
167 fail_fmt = "expected {!r}, got {!r}"
168 fail_msg = fail_fmt.format(expected, got)
169 fail_msg += ' ({})'.format(failure)
170 return fail_msg
171 else:
172 return None
173
174class FloatLike:
175 def __init__(self, value):

Callers 3

ftestMethod · 0.85
test_testfileMethod · 0.85
test_mtestfileMethod · 0.85

Calls 2

ulp_abs_checkFunction · 0.85
formatMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…