Hi folks,
During our workshop session this morning, one question in the chat window was about how to
(1) display a reduced fraction in the text of a webwork question and
(2) require that students enter their answer as a reduced fraction.
To accomplish (1), we want to use the reduce function provided by one of the standard PG macros (not to be confused with the reduce method for MathObjects, but that's a subtle point that can probably be safely ignored for right now). For instance, if $num and $den are scalar values for the numerator and denominator, the following code snippet produces an array ($num_r,$den_r) of reduced scalar values:
($num_r,$den_r) = reduce($num,$den);
For example, if $num=2 and $den=6, then $num_r=1 and $den_r=3.
To accomplish (2), we need to use the "contextFraction.pl" macro which helps with fraction specific answer checkers. Then, we could either use
Context("Fraction");
[...big code block...]
ANS( $answer->cmp(studentsMustReduceFractions=>1) );
or alternatively use a more restrictive context which requires reduced fractions by default
Context("LimitedFraction");
[...big code block...]
ANS( $answer->cmp() );
You can try out both of these by setting $case = 1; or $case = 2; below. The documentation for the "contextFraction.pl" macro is available at
http://webwork.maa.org/pod/pg_TRUNK/macros/contextFraction.pl.html
Best regards,
Paul Pearson
##########################################
# Begin PG file
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"contextFraction.pl",
"PGcourse.pl",
);
TEXT(beginproblem());
$case = 2;
if ($case == 1) { Context("Fraction"); } else { Context("LimitedFraction"); }
$a = -2;
$b = 6;
($a,$b) = reduce($a,$b); # $a and $b are Perl scalars (objects)
$answer = Compute("$a/$b"); # $answer is Fraction MathObject
Context()->texStrings;
BEGIN_TEXT
Enter \( $a / $b \):
\{ ans_rule(20) \}
END_TEXT
Context()->normalStrings;
$showPartialCorrectAnswers = 1;
if ($case == 1) {
ANS( $answer->cmp(studentsMustReduceFractions=>1) );
} else {
ANS( $answer->cmp() );
}
COMMENT('MathObject version.');
ENDDOCUMENT();