numbas / Numbas

A completely browser-based e-assessment/e-learning system, with an emphasis on mathematics
http://www.numbas.org.uk
Apache License 2.0
200 stars 118 forks source link

When marking equations, floating point error can break equality #865

Closed christianp closed 2 years ago

christianp commented 2 years ago

This question demonstrates the problem and a couple of solutions.

When the answer to a mathematical expression part is an equation, the values produced are booleans. Authors should set the value generators for variables to produce values that satisfy the equation about half of the time, but different forms of the correct expression can produce slightly different values due to floating point error, which would mean the equation is not satisfied.

I've come up with two solutions to this:

I prefer the second option, since it matches what the marking algorithm does for non-equations.

christianp commented 2 years ago

Should also deal with comparisons.

christianp commented 2 years ago

The equality test for numbers already uses isclose, so I was a bit confused about how this bug could occur.

After lots of digging, this is the problem: when evaluating a=b, the logic eventually uses isclose, which allows both absolute and relative tolerances. When both sides are quite big, the relative tolerance is generous. If you rearrange the equation so that both sides are quite small, the relative tolerance is smaller, so the comparison fails.

I think this has to be a "be aware of" problem, rather than something we can avoid automatically.