Merge pull request #1553 from MITx/fix/will/symbolic_response_does_not_accept_zero
Fixed an error in symmath_check
This commit is contained in:
@@ -149,6 +149,12 @@ def make_error_message(msg):
|
||||
msg = '<div class="capa_alert">%s</div>' % msg
|
||||
return msg
|
||||
|
||||
def is_within_tolerance(expected, actual, tolerance):
|
||||
if expected == 0:
|
||||
return (abs(actual) < tolerance)
|
||||
else:
|
||||
return (abs(abs(actual - expected) / expected) < tolerance)
|
||||
|
||||
#-----------------------------------------------------------------------------
|
||||
# Check function interface, which takes pmathml input
|
||||
#
|
||||
@@ -217,14 +223,14 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None
|
||||
# do a numerical comparison if both expected and answer are numbers
|
||||
if (hasattr(fexpect, 'is_number') and fexpect.is_number
|
||||
and hasattr(fans, 'is_number') and fans.is_number):
|
||||
if abs(abs(fans - fexpect) / fexpect) < threshold:
|
||||
if is_within_tolerance(fexpect, fans, threshold):
|
||||
return {'ok': True, 'msg': msg}
|
||||
else:
|
||||
msg += '<p>You entered: %s</p>' % to_latex(fans)
|
||||
return {'ok': False, 'msg': msg}
|
||||
|
||||
if do_numerical: # numerical answer expected - force numerical comparison
|
||||
if abs(abs(fans - fexpect) / fexpect) < threshold:
|
||||
if is_within_tolerance(fexpect, fans, threshold):
|
||||
return {'ok': True, 'msg': msg}
|
||||
else:
|
||||
msg += '<p>You entered: %s (note that a numerical answer is expected)</p>' % to_latex(fans)
|
||||
|
||||
28
lms/lib/symmath/test_symmath_check.py
Normal file
28
lms/lib/symmath/test_symmath_check.py
Normal file
@@ -0,0 +1,28 @@
|
||||
from unittest import TestCase
|
||||
from symmath_check import symmath_check
|
||||
|
||||
class SymmathCheckTest(TestCase):
|
||||
def test_symmath_check_integers(self):
|
||||
number_list = [i for i in range(-100, 100)]
|
||||
self._symmath_check_numbers(number_list)
|
||||
|
||||
def test_symmath_check_floats(self):
|
||||
number_list = [i + 0.01 for i in range(-100, 100)]
|
||||
self._symmath_check_numbers(number_list)
|
||||
|
||||
def _symmath_check_numbers(self, number_list):
|
||||
|
||||
for n in number_list:
|
||||
|
||||
# expect = ans, so should say the answer is correct
|
||||
expect = n
|
||||
ans = n
|
||||
result = symmath_check(str(expect), str(ans))
|
||||
self.assertTrue('ok' in result and result['ok'],
|
||||
"%f should == %f" % (expect, ans))
|
||||
|
||||
# Change expect so that it != ans
|
||||
expect += 0.1
|
||||
result = symmath_check(str(expect), str(ans))
|
||||
self.assertTrue('ok' in result and not result['ok'],
|
||||
"%f should != %f" % (expect, ans))
|
||||
Reference in New Issue
Block a user