WeBWorK Main Forum

num_cmp doesn't like LaTeX formatted numbers

by Peter Kootsookos -
Number of replies: 8

I have a circuit analysis question that I solve using a matrix inverse.

$M1 = Matrix([($R1+$R2),($R4+$R5)],[$W*$R2-1,1]);$V1 = Vector($VS2,0);$CurrentAns = $M1->inverse *$V1;

Then, I get the two answers:

$I1ans = Real($CurrentAns->element(1,1));$I2ans = Real($CurrentAns->element(2,1));

and try to compare them with the student-entered answer:

ANS(num_cmp("$I1ans", units => 'A',reltol=>1));ANS(num_cmp("$I2ans", units => 'A',reltol=>1));

However, if I "print" out the values I get:

 -5.39287\times 10^{-8} and        0.000647091

so the first num_cmp complains about the \.

The second num_cmp works fine (once I remove the $I1ans from the first num_cmp). Any ideas what I'm doing wrong to get the LaTeX formatting for the first element? In reply to Peter Kootsookos Re: num_cmp doesn't like LaTeX formatted numbers by Peter Kootsookos - num_cmp gives: Unexpected character '\'; see position 9 of formula at line 392 of [PG]/macros/PGnumericevaluators.pl Died within main::NUM_CMP called at line 392 of [PG]/macros/PGnumericevaluators.pl from within main::num_cmp called at line 126 of (eval 6204) In reply to Peter Kootsookos Re: num_cmp doesn't like LaTeX formatted numbers by Peter Kootsookos - Got a workaround: $I1ans = sprintf("%.4e",$CurrentAns->element(1,1));$I2ans = sprintf("%.4e",$CurrentAns->element(2,1)); But would still like to understand why it's LaTeXing in some cases and not others. In reply to Peter Kootsookos Re: num_cmp doesn't like LaTeX formatted numbers by Alex Jordan - I am guessing that is giving LaTeX in both of those cases, and in one case it is just not a deep enough number to trigger scientific notation. Do you have Context()->texStrings earlier in the problem code, but have not yet gone back to Context()->normalStrings? If that is the case, then the double quotes around the Math Object produce the LaTeX string for that number instead of the perl string. In reply to Alex Jordan Re: num_cmp doesn't like LaTeX formatted numbers by Peter Kootsookos - Yes, that seems to have been the problem! Context()->normalStrings; before I need to use num_cmp seems to do the trick! In reply to Peter Kootsookos Re: num_cmp doesn't like LaTeX formatted numbers by Alex Jordan - One thing that I would note is that you are working with Math Objects, a relatively modern (~15 yrs old or so) tool in WeBWorK. But the num_cmp() answer checker is from before the era of Math Objects. Instead, you could use the more Math Objects-y way to do the answer checking. I have not tried, but I think that if you add loading parserNumberWithUnits.pl to the libraries that are loaded, and then do like: $I1ans = NumberWithUnits($CurrentAns->element(1,1), 'amp'); and then later like: ANS($I1ans->cmp());
This should work. That requires using "amp" for the unit. If you wand "A" to be a valid alternative, you can add that.

$newUnit = {name => 'A', conversion => {factor =>1, amp=>1}}; $I1real = Real($CurrentAns->element(1,1));$I1ans = NumberWithUnits("$I1real A", {newUnit=>$newUnit});

Again, this is all untested. And some of it requires a recent version of WeBWorK.

If something about it does not work, post here and I will look more closely. (And in that case let me know what your WW version is.)

Re: num_cmp doesn't like LaTeX formatted numbers

by Peter Kootsookos -
Thank-you! That expands my understanding of how things connect. Much appreciated.

Re: num_cmp doesn't like LaTeX formatted numbers

by Danny Glin -
I would go a step farther and recommend to avoid using num_cmp completely. The old-style answer checkers are still around for backwards-compatibility reasons, and are not being updated to support new features of MathObjects.

There are a couple of downsides to using ANS(num_cmp("\$I1ans", units => 'A',reltol=>1)); on a MathObject rather than the approach that Alex recommends:
1. You are likely losing precision on your correct answer.  By default MathObjects Real numbers are displayed on the screen with a fixed number of digits, so the code above re-processes the rounded correct answer, and uses that to check the student answers.
2. There are a number of parameters associated with a MathObject that get lost when the object is displayed as a string, and then the string is used in num_cmp.  The LaTeX displays string, and any tolerance that was previously set are a couple of examples.