WeBWorK Main Forum

does tolerance in context affects conditional tests

does tolerance in context affects conditional tests

by Joel Trussell -
Number of replies: 1
We wrote a problem and set the tolerance for the answer checking as
Context("Complex");
Context()->flags->set(
tolerance => 0.01,
tolType => "absolute",
);

in determining the angle of a complex number, we wanted to set the answer equal to zero if the magnitude was zero - of close enough to zero. So we had a test
if ($magnitude_A < 0.001) {
$angle_A = Real(0);
}

In my experiments, I seem to have found that if $magnitude_A = 0 (or 10^-14), the test fails and I get a somewhat random angle, not zero. If I change the test to $magnitude_A < 0.1 the test works and I get $angle_A = 0; or If keep the test at $magnitude_A < 0.001 and change the tolerance to tolerance => 0.0001, the test works.

I thought the tolerance was only on the answers, but it appears to be on all logical tests. Is this correct?
is the only way to guard against this by setting the answer tolerance in the checking statement, e.g.,
ANS($magnitude_A->cmp(tolerance => 0.01,tolType => "absolute"));

Thanks
In reply to Joel Trussell

Re: does tolerance in context affects conditional tests

by Danny Glin -
I believe the answer to your question is yes. When comparing MathObject Reals it considers them equal if they are within the tolerance. Be careful that this applies to MathObjects, but not perl variables.

e.g.
$a = Real(0.00001);
$b = Real(0.00002);
$a == $b should be true with the tolerances you set, whereas

$c = 0.00001;
$d = 0.00002;
$c == $d should be false.