WeBWorK Problems

Numerical compute difficulty

Re: Numerical compute difficulty

by Davide Cervone -
Number of replies: 0
It depends on what result you want. Without quotes, perl will do the computations before passing the result to Compute(), which will then turn the result into a MathObject Real (unless the perl computation already involves MathObjects and results in some other MathObject type). It is perfectly fine to do that, and it is somewhat more efficient since it does not involve the expensive step of parsing the expression by the MathObject parser. For a Real value, that is probably a reasonable thing to do, as long as you remember things like the difference (in perl) between ^ and **.

When you pass a string to Compute(), it will first parse it using the MathObject parser, as controlled by the current Context object. That means that the operations and functions and so on that are defined there will be the ones that control the result, no the built-in perl expression parser. That makes the result work exactly like student answers, which is good for consistency, and also makes it easier to produce results that are not easy to produce in perl (like Intervals, and Vectors). Finally, Compute() saves the original string as the correct answer string, so that if you used

    $ans = Compute("sqrt(2)");
to produce a MathObject that was used for an answer checker via
    ANS($ans->cmp);
then the correct answer would show as sqrt(2) rather than 1.41421 as it would have been had you used Compute(sqrt(2)) without the quotes.

I don't think there is a hard-and-fast rule for when to use quotes and when not to. You have to think about what result you want to have in terms of the correct answer as well as the computed result.

Hope that clears things up for you.

Davide