1# test math.isclose (appeared in Python 3.5) 2 3try: 4 from math import isclose 5except ImportError: 6 print("SKIP") 7 raise SystemExit 8 9 10def test(a, b, **kwargs): 11 print(isclose(a, b, **kwargs)) 12 13 14def test_combinations(a, b, **kwargs): 15 test(a, a, **kwargs) 16 test(a, b, **kwargs) 17 test(b, a, **kwargs) 18 test(b, b, **kwargs) 19 20 21# Special numbers 22test_combinations(float("nan"), 1) 23test_combinations(float("inf"), 1) 24test_combinations(float("-inf"), 1) 25 26# Equality 27test(1.0, 1.0, rel_tol=0.0, abs_tol=0.0) 28test(2.35e-100, 2.35e-100, rel_tol=0.0, abs_tol=0.0) 29test(2.1234e100, 2.1234e100, rel_tol=0.0, abs_tol=0.0) 30 31# Relative tolerance 32test(1000.0, 1001.0, rel_tol=1e-3) 33test(1000.0, 1001.0, rel_tol=1e-4) 34test(1000, 1001, rel_tol=1e-3) 35test(1000, 1001, rel_tol=1e-4) 36test_combinations(0, 1, rel_tol=1.0) 37 38# Absolute tolerance 39test(0.0, 1e-10, abs_tol=1e-10, rel_tol=0.1) 40test(0.0, 1e-10, abs_tol=0.0, rel_tol=0.1) 41 42# Bad parameters 43try: 44 isclose(0, 0, abs_tol=-1) 45except ValueError: 46 print("ValueError") 47try: 48 isclose(0, 0, rel_tol=-1) 49except ValueError: 50 print("ValueError") 51