There are several different ways to accomplish.
If you are creating a constant rather than a formula, you can use
$r = Compute("sqrt(2)");
or
$V = Compute("<sqrt(2)+1,1/5>");
and the resulting object will use whatever string you originally
entered as its version of the "correct answer". (However, if you use $V
to generate its string version, it will still use the computed form.)
For formulas, the Parser usually reduces constant operations and function calls to their results, you could do something like Formula("$n x^($n-1)") and obtain Formula("3 x^2") when $n is 3, for example. To prevent this, use
Context()->flags->set(reduceConstants=>0,reduceConstantFunctions=>0);
which will turn off reducing operations between constants (like $n-1 in the previous example), and function calls with constant parameters (like your sqrt(2) ).
Note that this works only with formulas, since the constant objects
don't retain a parsed form, and so only keep the computed value. But
you have to be a bit careful about using Formulas for storing
constants, because the error messages a student will receive will say
that the answer is looking for a formula, and that could be confusing.
You really want to have the answer checker use the Real() answer
checker, not the Formula() one. Here's one way to do it:
Context()->flags->set(reduceConstants=>0,reduceConstantFunctions=>0);
$sq2 = Formula("sqrt(2)");
Context()->texStrings; BEGIN_TEXT \($sq2\) = \{ans_rule(10)\} END_TEXT Context()->normalStrings;
ANS(Compute($sq2)->cmp);
Here, $sq2 will be a formula that retains the sqrt , and so when it is inserted into the BEGIN_TEXT/END_TEXT block, it will produce \sqrt{2} rather than 1.41421. But, since you want the answer checked as a Real(), not a Formula(), you call Compute()
to turn the result into the actual constant, but the string used to
specify the value is retained and shown as the correct answer by the
answer checker. Since you have passed $sq2 to Compute() , it gets converted to a string, and then is computed, so the effect is the same as Compute("sqrt(2)") , which is what you wanted.
<| Post or View Comments |>
|