From 808712ba6592233025d261c70e5ca1b14bb74590 Mon Sep 17 00:00:00 2001 From: Martin Date: Tue, 6 Oct 2015 17:07:58 +0200 Subject: Fixed a bug with function almost_equal. --- python/util.py | 11 +++++++---- 1 file 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 prec 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): -- cgit v1.2.1