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)
| 173 | |
| 174 | |
| 175 | def result_check(expected, got, ulp_tol=5, abs_tol=0.0): |
| 176 | # Common logic of MathTests.(ftest, test_testcases, test_mtestcases) |
| 177 | """Compare arguments expected and got, as floats, if either |
| 178 | is a float, using a tolerance expressed in multiples of |
| 179 | ulp(expected) or absolutely (if given and greater). |
| 180 | |
| 181 | As a convenience, when neither argument is a float, and for |
| 182 | non-finite floats, exact equality is demanded. Also, nan==nan |
| 183 | as far as this function is concerned. |
| 184 | |
| 185 | Returns None on success and an error message on failure. |
| 186 | """ |
| 187 | |
| 188 | # Check exactly equal (applies also to strings representing exceptions) |
| 189 | if got == expected: |
| 190 | if not got and not expected: |
| 191 | if math.copysign(1, got) != math.copysign(1, expected): |
| 192 | return f"expected {expected}, got {got} (zero has wrong sign)" |
| 193 | return None |
| 194 | |
| 195 | failure = "not equal" |
| 196 | |
| 197 | # Turn mixed float and int comparison (e.g. floor()) to all-float |
| 198 | if isinstance(expected, float) and isinstance(got, int): |
| 199 | got = float(got) |
| 200 | elif isinstance(got, float) and isinstance(expected, int): |
| 201 | expected = float(expected) |
| 202 | |
| 203 | if isinstance(expected, float) and isinstance(got, float): |
| 204 | if math.isnan(expected) and math.isnan(got): |
| 205 | # Pass, since both nan |
| 206 | failure = None |
| 207 | elif math.isinf(expected) or math.isinf(got): |
| 208 | # We already know they're not equal, drop through to failure |
| 209 | pass |
| 210 | else: |
| 211 | # Both are finite floats (now). Are they close enough? |
| 212 | failure = ulp_abs_check(expected, got, ulp_tol, abs_tol) |
| 213 | |
| 214 | # arguments are not equal, and if numeric, are too far apart |
| 215 | if failure is not None: |
| 216 | fail_fmt = "expected {!r}, got {!r}" |
| 217 | fail_msg = fail_fmt.format(expected, got) |
| 218 | fail_msg += ' ({})'.format(failure) |
| 219 | return fail_msg |
| 220 | else: |
| 221 | return None |
| 222 | |
| 223 | class FloatLike: |
| 224 | def __init__(self, value): |
no test coverage detected