WeBWorK Problems

->{original_formula}

->{original_formula}

by Dick Lane -
Number of replies: 1
In a problem to solve an equation with nice values, I have matching arrays.

$k = random(1,2,1);
$t = ( 0 , '\frac{\pi}{6}', '\frac{\pi}{4}', '\frac{\pi}{3}' )[$k];
$y = ( 1 , '2/sqrt(3)'    , 'sqrt(2)'      , '2' )[$k];
$ans = Compute( "$y" );

Subsequent answer declaration displays typeset "correct"
ANS( $ans -> cmp() );

but solution shows decimal value even if I use
$sol = $ans->{original_formula} ;

A full example is attached.

I would prefer to avoid creation of an array with LaTex versions of the answers to display at end of Solution.
In reply to Dick Lane

Re: ->{original_formula}

by Davide Cervone -
Normally when a MathObject is computed, constants are reduced automatically, so that if you did
    $c = 2;
    Compute("x^(3*$c)");
you would get the equivalent of
    Compute("x^6");
This avoids your having to make separate variables for computed values within a formula.

This is good except in the situations where you really want to leave a constant in a symbolic rather than numeric form, as in your case. To handle that, MathObjects have two flags that control the reduction of constant values. So if you do

    Context("Numeric");
    Context->flags->set(
      reduceConstants => 0,
      reduceConstantFunctions => 0,
    );
before you call Compute(), that will preserve the original symbolic form in the {original_formula} value. E.g.,
    $ans = Compute("2/sqrt(3)");
would have $ans->{original_formula} as 2/sqrt(3) rather than 1.1547.

I noticed a couple of other items in your code. First, you note that

    $ans = List(Compute("$y"));
    ...
    $sol = $ans->{original_formula}
makes $sol be nothing. This is because there IS no original formula in this case, since you are calling List() directly on another MathObject (the result of the Compute() call. So no formula is parsed to do this. Only Compute() produces {original_formula}. (Of course, the element contained in the list has a {origin_formula}, so you could use ($ans->value)[0]->{original_formula} to get it.)

Second, you don't need to put $y in quotes, since it is already a string. This is redundant. You can just do

    $ans = Compute($y);

Third, you have set the tolerance to 0.0000000000001, which is very close to the limits of internal representations used by WeBWorK. (The numbers stored internally have 16 to 17 significant digits, and you are asking for 14 of those to be correct, but round-off errors and other numeric instability can easily affect the 13th digit.)

I suspect you are trying to force the answer to be given as a formula involving pi and square roots rather than as a decimal, but I would recommend using one of the means of preventing the student from entering a decimal explicitly rather than trying to require this kind of excessive precision. Try adding

    Parser::Number::NoDecimals;
after selecting the context.

Finally, since you are looking for symbolic answers, you might want to show the student's answer that way as well. So you might want to use

    Context()->flags->set(formatStudentAnswer => "parsed");
so that the student answer isn't reduced.

Hope that helps.

Davide