WeBWorK Problems

Allow fraction, decimal or percent, but show fraction as correct answer

Re: Allow fraction, decimal or percent, but show fraction as correct answer

by Alex Jordan -
Number of replies: 0
As you may be doing to get percents, try using contextPercent.pl (https://github.com/openwebwork/webwork-open-problem-library/blob/master/OpenProblemLibrary/macros/PCC/contextPercent.pl). Make $ans a percent object, but set the flags so that decimals and fractions are acceptable answers too. I think that's the default. That is, I think if $ans=Percent("50%"), a student can type 50%, 0.5, or 1/2 and be marked correct.

Then, to manipulate how to display the correct answer use the correct_ans_latex_string option when calling the answer checker. For example:
ANS($ans->cmp(correct_ans_latex_string=>'\frac{1}{2}'));

If you have $fractionanswer dynamically created as a MathObject Fraction, then I think this would work:
ANS($ans->cmp(correct_ans_latex_string=>($fractionanswer->TeX)));

So in summary, and completely untested:

loadMacros('contextFraction.pl', 'contextPercent.pl');
...
$realanswer = 0.5;
Context("Fraction");
$fractionanswer = Fraction($realanswer);
Context("Percent");
$ans = Percent($realanswer);
...
ANS($ans->cmp(correct_ans_latex_string=>($fractionanswer->TeX)));