## PREP 2015 Question Authoring - Archived

### The "Correct" answer is displaying as a constant, but should be a formula ### The "Correct" answer is displaying as a constant, but should be a formula

by tim Payer -
Number of replies: 3
Hello,

I have encountered a glitch in a homework problem that asks students to rationalize a denominator that holds a square root of x. The answer should include a rational expression that also holds a square root of x, However the response that one gets when entering the correct answer is the prompt:

"Your answer isn't a number (it looks like a formula that returns a number)"

On top of this the "correct answer" indicated by webwork is a constant (-0.4) in this example.

Can you see what might cause this glitch?

I have included the code of the problem and a screen picture with the errant response.

# DESCRIPTION  Problem 16
# Algebra_Review
# WeBWorK problem written by TimPayer <tsp1@humboldt.edu>
# ENDDESCRIPTION

## DBsubject(Algebra)
## DBsection(Rationalization of denominator)
## Institution(Humboldt State University)
## Author(Tim Payer)
## KEYWORDS(reduce, rationalization)

DOCUMENT();
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGbasicmacros.pl",
"problemRandomize.pl",
"parserFormulaUpToConstant.pl"
);

Context("Numeric");
$a = Real(random(3,9,1));$b = Real(random(2,9,1));
$c = Real(random(2,9,1));$csq = Compute("$c*$c");

Context()->variables->set(x => {limits => [[$a]/[$b],100]});

BEGIN_PGML
>>Simplify the given expression  <<
>>to a single term with a rational denominator<<

>>[\frac{[$a]-\sqrt{[$b]}}{\sqrt{x}-[$c]}] << >>= [__________]{([$a]-sqrt([$b]))*(sqrt(x)+[$c])/(x-[c**2])} << END_PGML BEGIN_PGML_SOLUTION *SOLUTION* To simplify the expression to a single term with a rational denominator we must start with rationalizing the denominator. Begin by multiplying the expression by the number one disguised as the conjugate of the denominator. [\begin{aligned}&\\ \frac{[a]-\sqrt{[$b]}}{\sqrt{x}-[$c]} &=\\
&=\frac{\left([$a]-\sqrt{[$b]}\right)\!\left(\sqrt{x}+[$c]\right)}{\left(\sqrt{x}-[$c]\right)\!\left(\sqrt{x}+[$c]\right)} && \text{Multiply top and bottom by the conjugate of the denominator.}\\ &=\frac{\left([$a]-\sqrt{[$b]}\right)\!\left(\sqrt{x}+[$c]\right)}{\sqrt{x}\cdot\sqrt{x}-[$c]\sqrt{x}+[$c]\sqrt{x}+[$c]\cdot \left(-[$c]\right)} && \text{Distribute through the denominator.}\\
&=\frac{\left([$a]-\sqrt{[$b]}\right)\!\left(\sqrt{x}+[$c]\right)}{x-[$csq]} && \text{Reduce and collect like terms.}
\end{aligned}]

END_PGML_SOLUTION

ENDDOCUMENT();  ### Re: The "Correct" answer is displaying as a constant, but should be a formula

by tim Payer -
One footnote to this glitch.

When we enter a zero for x we are rewarded with a "correct" answer statement.

Something unseen is causing the x variable to be set to zero, and therefore the problem reads the answer as a numeric value, not an expression that holds the variable of x.

Very troubling, any help is most appreciated...

Thanks, tim ### Re: The "Correct" answer is displaying as a constant, but should be a formula

by Davide Cervone -
There are a number of programming errors in your problem. The main one is that, in order to get a formula answer in PGML , you must enclose the formula in quotation marks. So you need to do
[__________]{"([$a]-sqrt([$b]))*(sqrt(x)+[$c])/(x-[$c**2])"}

not
[__________]{([$a]-sqrt([$b]))*(sqrt(x)+[$c])/(x-[$c**2])}

The contents of the braces is actually treated as a perl command whose result is used as the answer (this allows things like $f->cmp->withPostFilter(AnswerHints(...)) and so on to be used in the braces). So in your case, without the quotes, this is treated as a Perl expression and reduced to a constant before being used as the answer. Here, "x", will be treated as an unquoted string, which has value 0 in perl expressions. So you en up with a numerical result. (This is correct behavior, not a bug in PGML; the bug is in not putting the quotation marks around the expression.) If the result of evaluating the contents of the braces is a string (as it should be in your case) or a number, then PGML will call Compute() on the string and use the resulting MathObject as the answer. That is how you get formula answers. Note that A second problem is that you are using [$a] outside of PGML. Note that this does variable substitution only in PGML. Outside of PGML, it creates an array reference containing the value of $a. So in Context()->variables->set(x => {limits => [[$a]/[$b],100]});  both [$a] and [$b] are array references (i.e., pointers to arrays), not the values of the variables themselves. I think in this case, the result will be the fraction formed by the actual memory locations, not th values of the variables, so this is not the computation you want. You really want just Context()->variables->set(x => {limits => [$a/$b,100]});  as that is all that is required in perl expressions. Similarly, you have use [$a] in your answer where you only need $a, since the answer is treated as a perl string, not a PGML one. So you can use [__________]{"($a-sqrt($b))*(sqrt(x)+$c)/(x-\$csq)"}

for the answer. The other form is not an error, since brackets work as parentheses in the Numeric context, so the result is essentially the same, but it suggests a misunderstanding of the format and may lead to problems in other contexts.

A small detail is that you don't need to load PGbasicmacros.pl, since those are loaded automatically by PGstandard.pl. Also, you don't use problemRandomize.pl 