From 66b949b745b45b3a25ff32fdd23423ecaaff712c Mon Sep 17 00:00:00 2001 From: Syed Hassan Raza Date: Wed, 7 Jan 2015 17:01:38 +0500 Subject: [PATCH] Fix tolerance broken --- common/lib/capa/capa/tests/test_util.py | 9 +++++++++ common/lib/capa/capa/util.py | 17 ++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/common/lib/capa/capa/tests/test_util.py b/common/lib/capa/capa/tests/test_util.py index f6b76e1c9e..ecb6ea02c9 100644 --- a/common/lib/capa/capa/tests/test_util.py +++ b/common/lib/capa/capa/tests/test_util.py @@ -81,6 +81,15 @@ class UtilTest(unittest.TestCase): self.assertFalse(result) result = compare_with_tolerance(infinity, infinity, '1.0', False) self.assertTrue(result) + # Test absolute tolerance for smaller values + result = compare_with_tolerance(100.01, 100.0, 0.01, False) + self.assertTrue(result) + result = compare_with_tolerance(100.001, 100.0, 0.001, False) + self.assertTrue(result) + result = compare_with_tolerance(100.01, 100.0, '0.01%', False) + self.assertTrue(result) + result = compare_with_tolerance(100.002, 100.0, 0.001, False) + self.assertFalse(result) def test_sanitize_html(self): """ diff --git a/common/lib/capa/capa/util.py b/common/lib/capa/capa/util.py index e9197ad19e..ccbebd2a4b 100644 --- a/common/lib/capa/capa/util.py +++ b/common/lib/capa/capa/util.py @@ -5,6 +5,7 @@ import bleach from calc import evaluator from cmath import isinf +from decimal import Decimal #----------------------------------------------------------------------------- # # Utility functions used in CAPA responsetypes @@ -67,7 +68,21 @@ def compare_with_tolerance(student_complex, instructor_complex, tolerance=defaul else: # v1 and v2 are, in general, complex numbers: # there are some notes about backward compatibility issue: see responsetypes.get_staff_ans()). - return abs(student_complex - instructor_complex) <= tolerance + decimal_places = None + # count the "decimal_places" for "student_complex". e.g, for + # "student_complex" value "152.3667" the "decimal_places" will be + # 4 as there are 4 digits "3667" after decimal + if isinstance(student_complex, float): + decimal_places = Decimal(str(student_complex)).as_tuple().exponent * -1 # pylint: disable=E1103 + + abs_value = abs(student_complex - instructor_complex) + + # decimal_places could be NaN in some cases + if decimal_places and isinstance(decimal_places, int): + # abs_value contains 17 digits exponent value so + # round it up to "decimal_places" + abs_value = round(abs_value, decimal_places) + return abs_value <= tolerance def contextualize_text(text, context): # private