Hi All,
I've written a problem in which the answer to a part should be something like -5a + 9. (Full source below.) The expected answer for that part is controlled by this line:
$limit2 = Compute("a*$xval+$c2")->reduce();
I've noticed that if I use
$limit2 = Compute("a$xval+$c2")->reduce();
Then WW expects a-5+9 (and counts -5a+9 as incorrect but a+4 as correct).
On the other hand, if I use
a($xval), ${xval}a, etc, then it interprets this implied multiplication correctly. Similarly, the implied multiplication in this line
$f2 = Compute("ax + $c2")->reduce();
seems to work ok - it displays correctly and does what is intended if I pass it values for a and x.
So, I expected my original "a$xval" to be interpreted as implied multiplication. It wasn't, so I'm asking if this is the intended behavior, if I'm doing something wrong, etc.
Thanks,
Jason
DOCUMENT();
loadMacros(
"PGstandard.pl", # Standard macros for PG language
"MathObjects.pl",
"contextCurrency.pl",
"PGchoicemacros.pl",
#"source.pl", # allows code to be displayed on certain sites.
#"PGcourse.pl", # Customization file for the course
);
# Print problem number and point value (weight) for the problem
TEXT(beginproblem());
# Show which answers are correct and which ones are incorrect
$showPartialCorrectAnswers = 1;
##############################################################
#
# Setup
#
#
Context("Numeric");
Context()->variables->add(a=>'Real');
$c1 = non_zero_random(-9,9,1);
$c2 = non_zero_random(-9,9,1);
$xval = non_zero_random(-9,9,1);
$f1 = Compute("x^2 + $c1")->reduce();
$f2 = Compute("ax + $c2")->reduce();
$limit1 = $f1->eval(x=>$xval);
$limit2 = Compute("${xval}a+$c2")->reduce();
$a = ($limit1 - $c2)/$xval;
$i = random(0,3,1);
sub ineq {
if($i == 0) {
$ineq1 = $LTS;
$ineq2 = $GTS;
$cont = "No";
$extra = "Yes";
return ($ineq1,$ineq2,$cont,$extra);
} elsif($i == 1) {
$ineq1 = $LTS;
$ineq2 = $GTE;
$cont = "Yes";
$extra = "No";
return ($ineq1,$ineq2,$cont,$extra);
} elsif($i == 2) {
$ineq1 = $GTS;
$ineq2 = $LTS;
$cont = "No";
$extra = "Yes";
return ($ineq1,$ineq2,$cont,$extra);
} else {
$ineq1 = $GTS;
$ineq2 = $LTE;
$cont = "Yes";
$extra = "No";
return ($ineq1,$ineq2,$cont,$extra);
}
}
@cases = ineq();
$mc1 = new_multiple_choice();
$mc1->qa("If \(\displaystyle \lim_{x\rightarrow $xval} f(x) = $limit1 \), is \(f(x)\) a continuous function?", "$cases[2]");
$mc1->extra("$cases[3]");
##############################################################
#
# Text
#
#
Context()->texStrings;
BEGIN_TEXT
Let
\[
f(x) = \begin{cases}
$f1 &\text{ if } x \{@cases[0]\} $xval \\
$f2 &\text{ if } x \{@cases[1]\} $xval
\end{cases}
\]
(a) \(\displaystyle \lim_{x\rightarrow $xval^{+}} f(x) = \) \{ans_rule(5)\}
$PAR
(b) \(\displaystyle \lim_{x\rightarrow $xval^{-}} f(x) = \) \{ans_rule(5)\}
$PAR
(c) If we suppose that \(\displaystyle \lim_{x\rightarrow $xval} f(x) = $limit1\), then \(a = \) \{ans_rule(5)\}
$PAR
(d) \{ $mc1->print_q() \}
\{ $mc1->print_a() \}
END_TEXT
Context()->normalStrings;
##############################################################
#
# Answers
#
#
if($i > 1) {
ANS(Real($limit1) -> cmp());
ANS(Formula($limit2) -> cmp());
} else {
ANS(Real($limit2) -> cmp());
ANS(Formula($limit1) -> cmp());
}
ANS(Real($a) -> cmp());
ANS(radio_cmp( $mc1->correct_ans() ) );
ENDDOCUMENT();
This is the expected behavior, not a bug. The problem is in not being entirely clear on how Perl interprets what you have written. When you type
$limit2 = Compute("a$xval+$c2")->reduce();what happens first is that Perl replaces
$xval
into the string "a$xval+$c"
to produce the string "a-5+9"
. That gets done before Compute()
is called, and so the expression that is used to create the MathObject is "a-5+9"
which Compute()
correctly interprets as a+4. It has no idea that the -5 comes from a perl variable originally.
When you use
$limit2 = Compute("a$xval+$c2")->reduce();however, the string is
"a*-5+9"
, which is -5a+9. Alternatively,
$limit2 = Compute("a($xval)+$c2")->reduce();produces
"a(-5)+9"
which again is -5a+9, and similarly for the other forms.
Had you used a MathObject Real object rather than a Perl real, then it would have inserted the parentheses automatically itself, and it would have worked as you expected, but Perl reals don't. That is, if you had used
$c2 = Real(non_zero_random(-9,9,1)); $xval = Real(non_zero_random(-9,9,1)); $limit2 = Compute("a$xval+$c2")->reduce();you would have gotten
"a(-5)+9"
, which would have done what you wanted.
Hope that clears things up.
Davide