WeBWorK Problems

NumberWithUnits Failure

NumberWithUnits Failure

by Simon Wanderer -
Number of replies: 4
Hey guys,

I noticed my number with unit call fails when I use it with variables defined within maths mode like

$a = random(50, 90, 10);
$b = Compute("2*9.8*$a");
$v = Compute("sqrt{$b}") -> reduce();
$t = Compute("2*$v/9.8") -> reduce();


$velocity = NumberWithUnits($v, "m/s");
$time = NumberWithUnits($t, "s");

When I use the answer checkers below, it returns just the values of the variables as answers, so that adding the units as part of the answer gives a warning sign that says "Variable [unit] is not defined in this context"

ANS(num_cmp($velocity));
ANS( num_cmp($Answer2) );

Now when I used the same NumberWithUnits function in some other question without math mode answer, the answers are stored with units. Am I doing something wrong, if so, how can I right it?.

Thanks,

Simon.
In reply to Simon Wanderer

Re: NumberWithUnits Failure

by Alex Jordan -
Try:

ANS($velocity->cmp());
ANS($Answer2->cmp());

and see if the problem persists. Failing that, try

$velocity = NumberWithUnits("$v", "m/s");
$time = NumberWithUnits("$t", "s");

to pass strings to NumberWithUnits instead of complete Math Objects.
In reply to Alex Jordan

Re: NumberWithUnits Failure

by Simon Wanderer -
Thanks @ Alex Jordan

I have tried both your suggestions before I posted to this forum, the first returned my expression for velocity in unreduced form e.g. \root{somenumber} without unit, the second gave me same as using a math object anyways.

From what I have seen on the documentation on this wiki, there's a conflicting idea of how the function should be called; 

1. NumberWithUnits("$v", "m/s");
2. NumberWithUnits($v, "m/s");
3. NumberWithUnits("$v m/s");

All failed in my case.
In reply to Simon Wanderer

Re: NumberWithUnits Failure

by Alex Jordan -
Hi Simon,

In my testing, if $a, $b, $v, and $velocityN are defined to be:

$a = random(50, 90, 10);
$b = Compute("2*9.8*$a");
$v = Compute("sqrt{$b}") -> reduce();
$velocity1 = NumberWithUnits("$v", "m/s");
$velocity2 = NumberWithUnits($v, "m/s");
$velocity3 = NumberWithUnits("$v m/s");

Then with seed 1684:

TEXT($velocity1,$PAR,$velocity2,$PAR,$velocity3,$PAR);
prints all three as
37.0405 m/s

And for all three, if I enter "37.0405 m/s" as an answer in the following, it is counted as correct without any error that "m" is a variable not defined in the context.

BEGIN_TEXT
\{ans_rule(10)\}
$PAR
\{ans_rule(10)\}
$PAR
\{ans_rule(10)\}
$PAR
END_TEXT
ANS($velocity1->cmp());
ANS($velocity2->cmp());
ANS($velocity3->cmp());

However, with $velocity2, the correct answer column of the feedback table does not show "37.0405 m/s" (which is what $velocity1 and $velocity3 show). Instead, it shows the LaTeX from \sqrt{1372}. The correct_ans_latex_string is being derived from the original Compute call on $v. That is probably a bug.

If I change all the use of Compute() here to use Real() instead, then the issue with the correct_ans_latex_string is not present. All three show "37.0405 m/s" in the correct answer column.

This issue with the correct_ans_latex_string in the correct answer column is the only part I can reproduce. I don't get the unwanted \sqrt{1372} when I print these expressions. And I don't get messages about "m" being undefined anywhere from entering "37.0405 m/s" or entering "sqrt(1372) m/s".

Can you post a full (but maybe minimal) problem file where you are seeing this behavior? Also, what version of webwork and pg are you using?

Alex
In reply to Alex Jordan

Re: NumberWithUnits Failure

by Davide Cervone -
I have made a pull request to fix the problem with missing until in the second case. With the fix, that one should show the correct answer with a square root and units.