-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathis_close.py
103 lines (77 loc) · 3.5 KB
/
is_close.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#!/usr/bin/env python3
"""
Test implementation for an isclose() function, for possible inclusion in
the Python standard library -- PEP0485
This version has multiple methods in it for experimentation and testing.
The "final" version can be found in isclose.py
This implementation is the result of much discussion on the python-ideas list
in January, 2015:
https://mail.python.org/pipermail/python-ideas/2015-January/030947.html
https://mail.python.org/pipermail/python-ideas/2015-January/031124.html
https://mail.python.org/pipermail/python-ideas/2015-January/031313.html
Copyright: Christopher H. Barker
License: Apache License 2.0 http://opensource.org/licenses/apache2.0.php
"""
import cmath
def isclose(a,
b,
rel_tol=1e-9,
abs_tol=0.0,
method='weak'):
"""
returns True if a is close in value to b. False otherwise
:param a: one of the values to be tested
:param b: the other value to be tested
:param rel_tol=1e-8: The relative tolerance -- the amount of error
allowed, relative to the magnitude of the input
values.
:param abs_tol=0.0: The minimum absolute tolerance level -- useful for
comparisons to zero.
:param method: The method to use. options are:
"asymmetric" : the b value is used for scaling the tolerance
"strong" : The tolerance is scaled by the smaller of
the two values
"weak" : The tolerance is scaled by the larger of
the two values
"average" : The tolerance is scaled by the average of
the two values.
NOTES:
-inf, inf and NaN behave similar to the IEEE 754 standard. That
-is, NaN is not close to anything, even itself. inf and -inf are
-only close to themselves.
Complex values are compared based on their absolute value.
The function can be used with Decimal types, if the tolerance(s) are
specified as Decimals::
isclose(a, b, rel_tol=Decimal('1e-9'))
See PEP-0485 for a detailed description
"""
if method not in ("asymmetric", "strong", "weak", "average"):
raise ValueError('method must be one of: "asymmetric",'
' "strong", "weak", "average"')
if rel_tol < 0.0 or abs_tol < 0.0:
raise ValueError('error tolerances must be non-negative')
if a == b: # short-circuit exact equality
return True
# use cmath so it will work with complex or float
if cmath.isinf(a) or cmath.isinf(b):
# This includes the case of two infinities of opposite sign, or
# one infinity and one finite number. Two infinities of opposite sign
# would otherwise have an infinite relative tolerance.
return False
diff = abs(b - a)
if method == "asymmetric":
return (diff <= abs(rel_tol * b)) or (diff <= abs_tol)
elif method == "strong":
return (((diff <= abs(rel_tol * b)) and
(diff <= abs(rel_tol * a))) or
(diff <= abs_tol))
elif method == "weak":
return (((diff <= abs(rel_tol * b)) or
(diff <= abs(rel_tol * a))) or
(diff <= abs_tol))
elif method == "average":
return ((diff <= abs(rel_tol * (a + b) / 2) or
(diff <= abs_tol)))
else:
raise ValueError('method must be one of:'
' "asymmetric", "strong", "weak", "average"')