WeBWorK Problems

contextFraction.pl

Re: contextFraction.pl

by Davide Cervone -
Number of replies: 0
There are several problems here. First, the requireProperFractions flag requires the strictFractions flag, so it is really only effective in the LimitedFraction context, not in the plain Fraction context. Also, if you were to get this flag to work Fraction context (by setting strictFraction), then you would not be able to enter the improper fractions that you do in the Compute() calls below.

Second, the LimitedFraction context that you set at the bottom has no effect. This is because MathObjects retain the context in which they were created, so since $ans1, $ans2, and $ans3 where created in the Fraction context, they continue to be in that context even after you change the current context to LimitedFraction.

The proper way to do this is to create them in LimitedFraction context initially (without the requireReducedFractions flag set, so the correct answer can be entered as an improper fraction) and then set the requireReducedFractions flag after they are created.

Here is the code:


    #DESCRIPTION
    ##Type of
    #ENDDESCRIPTION

    DOCUMENT();
    loadMacros(
    "PGstandard.pl",
    #"PGchoicemacros.pl",
    #"PGgraphmacros.pl",
    "MathObjects.pl",
    # "compoundProblem.pl",
    #"contextCurrency.pl",
    #"contextInequalities.pl",
    #"unionTables.pl",
    # "unionLists.pl",
    #"unionMacros.pl",
    #"contextLeadingZero.pl",
    "contextFraction.pl",
    #"answerHints.pl",
    #"problemPanic.pl",
    );
    #for currency use Context("Currency") then Currency($A);
    #Then, in the text use $DOLLAR $a
    TEXT(beginproblem());
    $showPartialCorrectAnswers = 1;

    Context("LimitedFraction");

    $a1=random (3,7,2);
    $b1=random (4,8,4);
    $x=Compute("$a1/$b1");
    $a2=random (4,8,2);
    $b2=random (5,7,2);
    $y=Compute("$a2/$b2");
    $a3=random (3,9,3);
    $b3=random (5,10,5);
    $z=Compute("$a3/$b3");
    $ans1=$x-$y;
    $ans2=$x+2*$y;
    $ans3=$x+$y+$z;
    #warn "a1=$a1, b1=$b1, x=$x, a2=$a2, b2=$b2, y=$y, a3=$a3, b3=$b3, z=$z, a1=$ans1";

    Context()->flags->set(requireProperFractions => 1);


    BEGIN_TEXT
    Evaluate each expression if \(x =\frac{$a1}{$b1}\), \(y=\frac{$a2}{$b2}\), and
    \(z=\frac{$a3}{$b3}\).$BR
    Your answer should be a reduced fraction or a mixed number with the $BR
    faction part reduced (a mixed number can be entered in the form 2 3/4 to
    mean \(2\frac{3}{4}\)).$PAR
    $PAR
    a) \(x-y=\) \{ans_rule(6)\}
    $PAR
    b) \(x+2y=\) \{ans_rule(6)\}
    $PAR
    c) \(x+y+z=\) \{ans_rule(6)\}
    END_TEXT

    ANS($ans1->cmp);
    ANS($ans2->cmp);
    ANS($ans3->cmp);

    ENDDOCUMENT();

Hope that clears things up.

Davide