Commit a1db394b by Peter Baratta

Test for infinity in numerical and formula responses

parent f970bbd1
...@@ -1869,8 +1869,6 @@ class FormulaResponse(LoncapaResponse): ...@@ -1869,8 +1869,6 @@ class FormulaResponse(LoncapaResponse):
log.debug('formularesponse: error %s in formula' % err) log.debug('formularesponse: error %s in formula' % err)
raise StudentInputError("Invalid input: Could not parse '%s' as a formula" % raise StudentInputError("Invalid input: Could not parse '%s' as a formula" %
cgi.escape(given)) cgi.escape(given))
if numpy.isnan(student_result) or numpy.isinf(student_result):
return "incorrect"
if not compare_with_tolerance(student_result, instructor_result, self.tolerance): if not compare_with_tolerance(student_result, instructor_result, self.tolerance):
return "incorrect" return "incorrect"
return "correct" return "correct"
......
from .calc import evaluator, UndefinedVariable from .calc import evaluator, UndefinedVariable
from cmath import isinf
#----------------------------------------------------------------------------- #-----------------------------------------------------------------------------
# #
...@@ -20,8 +21,11 @@ def compare_with_tolerance(v1, v2, tol): ...@@ -20,8 +21,11 @@ def compare_with_tolerance(v1, v2, tol):
tolerance = tolerance_rel * max(abs(v1), abs(v2)) tolerance = tolerance_rel * max(abs(v1), abs(v2))
else: else:
tolerance = evaluator(dict(), dict(), tol) tolerance = evaluator(dict(), dict(), tol)
return abs(v1 - v2) <= tolerance
if isinf(v1) or isinf(v2):
return v1 == v2 # because the other numerical comparison does not work with infinities
else:
return abs(v1 - v2) <= tolerance
def contextualize_text(text, context): # private def contextualize_text(text, context): # private
''' Takes a string with variables. E.g. $a+$b. ''' Takes a string with variables. E.g. $a+$b.
......
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