diff options
author | Martin <martin@leo.fri1.uni-lj.si> | 2015-10-06 17:07:58 +0200 |
---|---|---|
committer | Martin <martin@leo.fri1.uni-lj.si> | 2015-10-06 17:07:58 +0200 |
commit | 808712ba6592233025d261c70e5ca1b14bb74590 (patch) | |
tree | 78c74a2bf6b1e1702fad6467cf4a24bd3de66d15 | |
parent | 2768eb14d8042206c47bdabdcc8ae14efe4e080b (diff) |
Fixed a bug with function almost_equal.
-rw-r--r-- | python/util.py | 11 |
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): |