next up previous contents
Next: Answer Evaluators Up: Gotchas Previous: Exponentiation   Contents

Using function_cmp

Here are two places in which is it easy to get caught. Neither are difficult to understand; both have happened to me (only once ... so far).

When using function_cmp, the function is defined as a string and you have to be careful about $variable substitution. For example, in the following if $a is a previously defined variable, say with $a = -2, you get an error in the following context:

$ans = "-$a*sin($a*x)";
ANS(function_cmp($ans));

The error would look something like:

Error message:

Tell your professor that there is an error in this problem.
ERROR: at line 1465 of file (eval 97) Can't modify constant item in
predecrement at (eval 109) line 1, near "2*" The calling
package is PG_priv

The problem is that the string $ans becomes -2*sin(-2*x), and - is the decrement operator. The solution is very easy, just put a space after the minus sign:
$ans = - $a*sin($a*x);. Perl is smart enough to know - -2 is 2. Defining
$ans = - ($a)*sin($a*x); would be another solution.

There is an analogous problem if you write:

$ans = "x+$a**2";
ANS(function_cmp($ans));

Again if $a = -2, this yields x+-2**2, i.e. x - 4 whereas you probably were expecting x + 4. The solution is the write $ans = x+($a)**2;.

The second place where one can go astray with function_cmp comes from forgetting how it is that two functions are tested for ``equality''. The functions are not analyzed symbolically; values are tested in each function and compared. The rub comes from the simpler invocations of function_cmp (see Sample Problem 2 in the section ``Learning to Walk'' for more details). That is, the default values are chosen from the interval $ (0,1)$. If the domain of your function does not contain this interval, errors will fly.

Consider the specific example:

$ans = sqrt(1- $a x^2);   ## Here $a can be bigger than 1
ANS(function_cmp($ans));

This will likely lead to errors. Instead, specify the allowable domain as in

$upper_limit = 1/sqrt(abs($a));
ANS(function_cmp($ans,"x",0, $upper_limit));

Note in this construct, the independent variable x must be specified.


next up previous contents
Next: Answer Evaluators Up: Gotchas Previous: Exponentiation   Contents
Thomas R. Shemanske 2002-03-05