PREP 2014 Question Authoring - Archived

Reduced fractions in problem text and in student answers

Reduced fractions in problem text and in student answers

by Paul Pearson -
Number of replies: 1
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();

In reply to Paul Pearson

Re: Reduced fractions in problem text and in student answers

by Davide Cervone -
Note that the Fraction context can help with (1) as well, as fractions are reduced automatically when they are created (by a professor). For example, if you enter
    loadMacros("contextFraction.pl");
    Context("Fraction");
    $r = Compute("4/6");
in the Interactive PG lab, you will get 2/3 as the output, not 4/6. There are flags that control the reduction of fractions, and other output features, like whether to show mixed numbers or improper fractions.

For student answers, the reduction is turned off automatically, so when you use studentMustReduceFractions => 1, their fractions aren't reduced by default.

See the documentation for the Fraction context for details.

So in terms of (1), you can just create a Fraction object and include it in your BEGIN_TEX/END_TEXT a you would any other MathObject, and it will format the reduced fraction automatically.