WeBWorK Problems

Fractions not Reducing

Fractions not Reducing

by Patrick Spencer -
Number of replies: 2
WW is having trouble reducing fractions in answers. I'm getting the following:

screen_shot.png
Here is the code:

###########################################################################
# initialization 
###########################################################################
DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PeriodicRerandomization.pl",
  "contextFraction.pl",
  # "fracListChecker.pl", # contains custom checker and restricts context
  "MUHelp.pl",
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1;
PeriodicRerandomization("3"); 


###########################################################################
# setup contexts and variables 
###########################################################################
Context()->strings->add("no solution"=>{}, "none"=>{alias=>"no solution"}, "no solutions"=>{alias=>"no solution"});
$var = list_random('x','y','z','w','p','q','r','s','t','u','v');
Context()->variables->are($var=>"Real", uc($var)=>"Real");

$a = list_random(-3,-2,2,3);
$b = ($a == 2 || $a == -2) ? list_random(3,4,5,6,10) : list_random(2,3,4,10);
$ba = ($b)**($a);

$eqn = "\log_{$b} ($var) = $a ";

Context("Fraction");
$answer = Fraction($ba);


###########################################################################
# state the problem 
###########################################################################
Context()->texStrings;
BEGIN_TEXT
Solve the equation for \($var\):
$PAR \[ $eqn \] $PAR
\( $var = \) \{ ans_rule(20) \}
END_TEXT
Context()->normalStrings;


###########################################################################
# check the answer  
###########################################################################
ANS($answer->cmp());


###########################################################################
# use PeriodicRerandomization to write the answer and generate a new
# version of the problem
###########################################################################
Context()->texStrings;
if ($attempts_modp == 0 && $actualAttempts != 0) {
  BEGIN_TEXT
  $PAR
  $BBOLD Answer: $EBOLD \($var = $answer \)
  $PAR
  END_TEXT
} else {
  BEGIN_TEXT
  $PAR
  $BBOLD Help: $EBOLD \{ MUHelp("logeqns") \}
  $BR
  END_TEXT
}
Context()->normalStrings;
PeriodicStatus();

COMMENT('Features Periodic Rerandomization. Edited and updated in 2012/2013.
$BR
Desc: Solve a logarithmic equation of the form log_b(x) = a, where a is nonzero.');
ENDDOCUMENT();

############################################################
End code
############################################################

For some reason it is not reducing 1/27 in the asnwer as it should. It does reduce other fraction such as 1/100.

Any help/explanation would be appreciated.

Thank you!
In reply to Patrick Spencer

Re: Fractions not Reducing

by Alex Jordan -
In your example, 1/27 is the answer. As a decimal, that is 0.037037037...

At the line
$answer = Fraction($ba);
you are passing a perl real to Fraction, and that is handled by only taking the first six digits after the decimal and placing them over 1000000 as a denominator. So this explains what you see. And it explains why most fractions whose denominators have prime factorizations with 2s and 5s only come out fine.

The development branch of pg has a new method I wrote for this kind of situation, which would result in your $answer being the Math Object Fraction(1,27). It finds a continued fraction approximant for the perl real. But I don't know how you could cleanly pull just that part from GitHub.

Instead, you could change your code to be more explicit about what the numerator and denominator are. Here, the following might work:
$answer = ($a > 0 ) ? Fraction($b**($a),1) : Fraction(1, $b**(-$a));
In reply to Alex Jordan

Re: Fractions not Reducing

by Patrick Spencer -
Changing it to

$answer = ($a > 0 ) ? Fraction($b**($a),1) : Fraction(1, $b**(-$a));

Worked! Thank you!