## WeBWorK Problems

### strange issue when a random numerator is 0

by roo biki -
Number of replies: 3

I'm writing a problem which asks students to simplify an expression of the form ax^b, where:

• a is randomly either 1 or -1,
• x and b are both random integers which are not too large, and
• b is not negative if x is zero.

I'm using the LimitedFraction context and assigning the answer flag studentMustReduceFractions.

The issue occurs when the base x happens to be equal to 0, and in this case the final answer (which should be 0) is not being correctly computed.  I'm having difficulties inserting the screenshot into the post, but I've included a link to the image here: https://imgur.com/a/YnxQmo9

For context, I've included the full problem as an attachment, but for the purpose of my question here is a MWE that produces the issue:

DOCUMENT();

"PGstandard.pl",
"contextFraction.pl",
"PGML.pl"
);

TEXT(beginproblem());

Context("Fraction");

$ans = Fraction(0,1)->cmp(studentsMustReduceFractions => 1);$ans = -$ans; BEGIN_PGML What does the fraction [\frac{0}{1}] reduce to? Answer: [___]{$ans}

END_PGML

ENDDOCUMENT();

The problem is caused only by the combination of applying the answer flag, and then replacing the answer with its negation.  If either the flag application or the negation assignment are removed, the problem disappears.

Fortunately this does yield a work-around (applied in the attachment), which consists of applying the answer flag only if x is not equal to 0, but I'm going crazy trying to figure out what exactly is happening here.

Can anyone explain this issue?  or is it possibly a bug?

### Re: strange issue when a random numerator is 0

by Danny Glin -

I can see why there might be issues with the code you provided, since I don't think that this is an expected use case.

The line

$ans = Fraction(0,1)->cmp(studentsMustReduceFractions => 1); stores the compare method associated with the MathObject "Fraction(0,1)" in the variable$ans, so when you subsequently call the variable $ans you are getting the comparison method and not the MathObject itself. This can be counterintuitive because PGML is set up to accept either a MathObject or a comparison method as the correct answer. When you do$ans = -$ans you are trying to take the negative of a compare method, and not of a MathObject, so I'm not surprised that there is weird behaviour. The safer thing to do is to store the compare method after you do any manipulations to the MathObject. In your MWE you could do$ans = Fraction(0,1);
$ans = -$ans;
$ans =$ans->cmp(studentsMustReduceFractions => 1);

I can't say whether this should be considered a bug or just an unsupported way to use MathObjects/PGML.  If you believe that it is a bug you can file an issue at https://github.com/openwebwork/pg/issues.

### Re: strange issue when a random numerator is 0

by Glenn Rice -

This is definitely not a bug.  You have to use the objects properly that you are working with.  It is simply an incorrect way to use MathObjects.

When you do $ans = -$ans you are trying to take the negative of a compare method, and not of a MathObject, so I'm not surprised that there is weird behaviour.