WeBWorK Problems

Bug in computing powers

Re: Bug in computing powers

by Davide Cervone -
Number of replies: 0
Thanks, that helped.

The issue is as I expected, the perl real is .0000518579035997391, which stringifies as 5.18579035997391e-05, which MathObjects interprets as involving the constant "e" rather than scientific notation (which it expects to be 5.18579035997391E-05).

There are several possible solutions:

  1. You could use
        $ans = Compute(uc($ans));
    
    to force the e to be converted to upper case before Compute() processes it. This will get the correct value to be computed.

Alternatively, you could use
    $ans = Real($ans);
which doesn't cause $ans to be converted to a string first (it simply makes the perl real into a MathObject Real). That avoids the parsing and misinterpretation of the "e".

But my favorite is probably to replace
    $ans = 0;
by
    $ans = Real(0);
so that $ans will be a MathObject Real right from the start, and the remaining computations are then performed as MathObject Real operations. In this case, there is no need for the Compute() call at all, so that can be removed.

Any of those should get you better results. Hope that does the trick for you.