Below is an MWE that checks for appropriate numerical precision. This is achieved with the Math::SigFigs perl module. The same module will be able to be called in problem files to calculate to 'appropriate' numerical precision sums, and differences (to least dp), and, products and ratios (to least sf). It will also be possible to count significant figures and format numbers to a set number of significant figures from the output of perl math functions.
Caveat
This is still a work in progress and doesn't have all the behaviour that I'd like. I also have not extensively tested the method.
After installing the perl module Math::SigFigs with the advice found in this post https://webwork.maa.org/moodle/mod/forum/discuss.php?d=8389 (with thanks to Glen Rice) I hacked together a custom answer checker.
what the checker does:
Given an answer passed to the answer checker the student input will be graded correct if
- the correct answer ($correct from answerHash) and student answer ($student from answerHash) numerically match AND
- the significant figures in the correct answer ($correct in the answer hash) and the entered student string ($ansHash->{original_student_ans} from the answer hash) also numerically match.
Test cases:
for 1.23456789876548 that is rounded to 5 dp using sprintf and passed to the answer checker
Desired behaviour
- 1.23457 is graded correct.
- 1.234570 is graded incorrect with the custom message. "Your answer should be expressed to the 'appropriate' numerical precision".
- 123457E-5 is graded correct.
- 123457.0E-5 is graded incorrect with the custom message.
- 0.0000123457E5 is graded correct.
- 0.00001234570E5 is graded incorrect with the custom message.
Undesired behaviour - these are mathematically correct but are graded incorrect with the custom message.
- 123457 * 10^-5
- 123457 * 10**-5
- 0.0000123457*10^5
- 0.0000123457*10**5
Clearly in a future iteration it would be nice to correct the undesired behaviour stated above. I've a feeling that it has something to do with how WeBWorK parses the number by using regex similar to the discussion in advanced contexts linked below? Maybe someone could enlighten me on this.
AFAIK the answer checker will only be useful to the WeBWorK default tolerance which I think is 5 decimal places. This can be increased with the Context()->{format}{number} in the MWE below. I haven't tested this to its limit but I think that it would be 15 dp from what I've read about how perl treats floating point arithmetic. More about Context()->{format}{number} here
Please if anyone spots further bugs/undesired behaviour or has some useful suggestions or changes on how to improve this method please let me know. Or better still if you have the time, write the bug fix or improvement in, and post on this thread.
#######################################################################################################
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl"
);
TEXT(beginproblem());
Context("Numeric");
#Context()->{format}{number} = "%.5f";
$val = 1.23456789876548;
$prec = 5;
$ans = Real(sprintf("%.${prec}f", $val));
ANS( Real($ans)->cmp(
(checker=>sub {
($correct, $student, $ansHash) = @_;
$correct_sf = Math::SigFigs::CountSigFigs($correct);
$student_sf = Math::SigFigs::CountSigFigs($ansHash->{original_student_ans});
Value->Error("Your answer should be expressed to the 'appropriate' numerical precision") if (($correct == $student) && ($correct_sf != $student_sf));
return (($correct == $student) && ($correct_sf == $student_sf) ? 1 : 0)
}),
)
);
Context()->texStrings;
BEGIN_TEXT
$PAR
express $val to $prec dp \{ ans_rule(25) \}
$PAR
ans = $ans
END_TEXT
Context()->normalStrings;
$showPartialCorrectAnswers = 1;
ENDDOCUMENT();