WeBWorK Problems

LimitedFraction: Is -8/2 reduced?

LimitedFraction: Is -8/2 reduced?

by Bruce Yoshiwara -
Number of replies: 3
I'm puzzled by what I'm seeing in the LimitedFraction context.

When the correct answer is -4/3, the checker does reject -8/6 and warns the student that his/her answer is not reduced.

But when I assign 0 as the correct answer, the checker accepts 0/5 as correct, and when I assign
-4 as the answer, the checker accepts -8/2.

I would like the checker to reject any rational expression m/n when the answer is actually an integer (when in the LimitedFraction context).

#-------
loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"contextFraction.pl",
);

Context("LimitedFraction");

$ans1 = Compute("-4/3");
$ans2 = Compute("0");
$ans3 = Compute("-4");

Context()->flags->set(formatStudentAnswer=>'parsed');
Context()->flags->set(reduceConstantFunctions=>0);
Context()->texStrings;
BEGIN_TEXT
$PAR
1. \{ ans_rule(4) \} 2. \{ ans_rule(4) \} 3. \{ans_rule(4) \}
END_TEXT
Context()->normalStrings;

ANS( $ans1->cmp(studentsMustReduceFractions=>1,showProperFractions=>0) );
ANS( $ans2->cmp(studentsMustReduceFractions=>1,showProperFractions=>0) );
ANS( $ans3->cmp(studentsMustReduceFractions=>1,showProperFractions=>0) );

#---

Thanks.

Bruce
In reply to Bruce Yoshiwara

Re: LimitedFraction: Is -8/2 reduced?

by Davide Cervone -
The problem is that $ans2 and $ans3 are Real MathObjects rather than Fraction MathObjects, and, of course, -8/2 as a real is the same as -4. You must force these integers to be Fraction objects in order to have them work the way you want:
    $ans1 = Fraction("-4/3");
    $ans2 = Fraction("0");
    $ans3 = Fraction("-4");
should work. (This is one of the few cases where you want to use the explicit constructor rather than Compute(), because you are coercing one object type to be another.)

Note also that

    Context()->flags->set(formatStudentAnswer=>'parsed');
    Context()->flags->set(reduceConstantFunctions=>0); 
are not needed, here(but may be used by other parts of your actual problem). In any case, you can combine them as
    Context()->flags->set(
      formatStudentAnswer=>'parsed',
      reduceConstantFunctions=>0,
    );
if you do need them.

Finally, note that studentsMustReduceFractions=>1, showProperFractions=>0 are the default for the LimitedFraction context, and so do not need to be given explicitly in the ANS() calls. You can just do

    ANS( $ans1->cmp );
    ANS( $ans2->cmp );
    ANS( $ans3->cmp );
and still have the same effect. If you really DO want to specify these values explicitly, you can add them to the context so that you don't have to enter them three times:
    Context()->flags->set(
      studentsMustReduceFractions=>1,
      showProperFractions=>0,
    );
    ANS( $ans1->cmp );
    ANS( $ans2->cmp );
    ANS( $ans3->cmp );
but again, these are already set in the LimitedFraction context.

Hope that does the trick for you.

Davide

In reply to Davide Cervone

Re: LimitedFraction: Is -8/2 reduced?

by Bruce Yoshiwara -
Davide,

Thanks, that helped.

But I found that I needed to keep

$ans1 = Compute("-4/3");

because when I used

$ans1 = Fraction("-4/3");

WeBWorK marked the answer -4/3 as incorrect, showing the correct answer as

-1333333/1000000

And then I did seem to need

Context()->flags->set(showProperFractions=>0 );

Things do now seem to work as I'd like.

Bruce
In reply to Bruce Yoshiwara

Re: LimitedFraction: Is -8/2 reduced?

by Davide Cervone -
OK, I almost left Compute() for that one, but you are right, you do need to keep it to avoid Fraction (unless you also set reduceConstants=>0 in the context flags, I think). My mistake about showProperFractions; I looked too quickly and saw that you are turning that OFF, which is NOT the default, as you have found.

I'm glad you have things working now.

Davide