Commit 9fb1c0b2 by Victor Shnayder

Merge pull request #1553 from MITx/fix/will/symbolic_response_does_not_accept_zero

Fixed an error in symmath_check
parents ae681a52 850d9846
...@@ -149,6 +149,12 @@ def make_error_message(msg): ...@@ -149,6 +149,12 @@ def make_error_message(msg):
msg = '<div class="capa_alert">%s</div>' % msg msg = '<div class="capa_alert">%s</div>' % msg
return 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 # Check function interface, which takes pmathml input
# #
...@@ -217,14 +223,14 @@ def symmath_check(expect, ans, dynamath=None, options=None, debug=None, xml=None ...@@ -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 # do a numerical comparison if both expected and answer are numbers
if (hasattr(fexpect, 'is_number') and fexpect.is_number if (hasattr(fexpect, 'is_number') and fexpect.is_number
and hasattr(fans, 'is_number') and fans.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} return {'ok': True, 'msg': msg}
else: else:
msg += '<p>You entered: %s</p>' % to_latex(fans) msg += '<p>You entered: %s</p>' % to_latex(fans)
return {'ok': False, 'msg': msg} return {'ok': False, 'msg': msg}
if do_numerical: # numerical answer expected - force numerical comparison 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} return {'ok': True, 'msg': msg}
else: else:
msg += '<p>You entered: %s (note that a numerical answer is expected)</p>' % to_latex(fans) msg += '<p>You entered: %s (note that a numerical answer is expected)</p>' % to_latex(fans)
......
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))
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment