summaryrefslogtreecommitdiff
path: root/python
diff options
context:
space:
mode:
Diffstat (limited to 'python')
-rw-r--r--python/util.py11
1 files changed, 7 insertions, 4 deletions
diff --git a/python/util.py b/python/util.py
index ecd5ee8..78fd362 100644
--- a/python/util.py
+++ b/python/util.py
@@ -16,9 +16,12 @@ def has_token_sequence(tokens, sequence):
return True
return False
-def almost_equal(a, b, prec=1):
- """ Compares values a and b using at most <code>prec</code> decimal values. """
- return round(a*10**prec) == round(b*10**prec)
+def almost_equal(a, b, prec):
+ """ Compares values a and b
+ using prec if value < 1 or
+ prec most significant numbers otherwise.
+ """
+ return abs(a-b) <= max(abs(a), abs(b), 1) * 10**(-prec)
def get_numbers(s):
""" Extracts numbers from string s. """
@@ -34,7 +37,7 @@ def get_numbers(s):
''', s, re.VERBOSE)
return [float(v) for v in str_vals]
-def string_almost_equal(s, a, prec=1):
+def string_almost_equal(s, a, prec=333):
""" Searches string s for a value that is almost equal to a. """
for v in get_numbers(s):
if almost_equal(v, a, prec):