From e1ba05b29456a25fc345049b534092f721c8e272 Mon Sep 17 00:00:00 2001 From: KarlLundengaard Date: Thu, 19 Oct 2023 14:15:09 +0100 Subject: [PATCH] Fixed potential bug when either atol or rtol was zero but not the other --- app/symbolic_comparison_evaluation.py | 4 ++-- app/symbolic_comparison_evaluation_tests.py | 14 +++++++++++--- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/app/symbolic_comparison_evaluation.py b/app/symbolic_comparison_evaluation.py index c11a180..57d94e7 100644 --- a/app/symbolic_comparison_evaluation.py +++ b/app/symbolic_comparison_evaluation.py @@ -283,7 +283,7 @@ def replace_pi(expr): return expr.subs(pi_symbol, float(pi)) ans = replace_pi(ans) res = replace_pi(res) - if "atol" in params.keys(): + if float(params.get("atol", 0)) > 0: try: absolute_error = abs(float(ans-res)) error_below_atol = bool(absolute_error < float(params["atol"])) @@ -291,7 +291,7 @@ def replace_pi(expr): error_below_atol = None else: error_below_atol = True - if "rtol" in params.keys(): + if float(params.get("rtol", 0)) > 0: try: relative_error = abs(float((ans-res)/ans)) error_below_rtol = bool(relative_error < float(params["rtol"])) diff --git a/app/symbolic_comparison_evaluation_tests.py b/app/symbolic_comparison_evaluation_tests.py index 9558013..efcb320 100644 --- a/app/symbolic_comparison_evaluation_tests.py +++ b/app/symbolic_comparison_evaluation_tests.py @@ -562,8 +562,8 @@ def test_empty_input_symbols_codes_and_alternatives(self): ), ( "Incorrect response, symbolic comparison set with atol=0", - "sqrt(3)+5", "6.73", + "sqrt(3)+5", {"atol": 0}, [], False @@ -594,8 +594,8 @@ def test_empty_input_symbols_codes_and_alternatives(self): ), ( "Incorrect response, symbolic comparison set with atol=0 and rtol=0", - "sqrt(3)+5", "6.73", + "sqrt(3)+5", {"rtol": 0, "atol": 0}, [], False @@ -608,6 +608,14 @@ def test_empty_input_symbols_codes_and_alternatives(self): ["WITHIN_TOLERANCE"], True ), + ( + "Correct response, tolerance specified with atol != 0 and rtol = 0", + "6.73", + "sqrt(3)+5", + {"atol": 0.005, "rtol": 0}, + ["WITHIN_TOLERANCE"], + True + ), ( "Incorrect response, tolerance specified with atol", "6.7", @@ -667,7 +675,7 @@ def test_empty_input_symbols_codes_and_alternatives(self): ] ) def test_numerical_comparison_problem(self, description, response, answer, tolerance, tags, outcome): - params = {"numerical": True} + params = {"elementary_functions": True} params.update(tolerance) result = evaluation_function(response, answer, params, include_test_data=True) assert result["is_correct"] is outcome