## WeBWorK Problems

### Bug in computing powers ### Bug in computing powers

by Gabriela Sanchis -
Number of replies: 3
I'm having quite a few issues with some problems in probability where the answer involves powers. Below is the code, which now works, but it doesn't work if I use $ans=compute($ans) and ANS($ans->cmp() ) to check my answer (I commented these out). For some students it works fine, but for others it thinks the correct answer is something involving e- it seems to be just converting scientific notation in some funky way. While the code below works, it does not allow students to use the C(n,k) notation to enter their answers, which is why I wanted to use the IntegerFunctions context. Any help would be greatly appreciated. TEXT(beginproblem());$showPartialCorrectAnswers = 1;

Context("IntegerFunctions");

$n = random(20,30,1);$k = random(int(.7*$n), int(.9*$n),1);

$ans = 0; for ($i = $k;$i<($n+1);$i++){
$ans =$ans + fact($n)/fact($i)/fact($n -$i)/2**$n; } #$ans=Compute($ans); BEGIN_TEXT A man claims to have extrasensory perception. As a test, a fair coin is flipped $$n$$ times, and the man is asked to predict the outcome in advance. He gets $$k$$ out of $$n$$ correct. What is the probability that he would have done at least this well if he had no ESP?$BR

\{ans_rule(20)\}

END_TEXT

#ANS($ans->cmp() ); ANS(num_cmp($ans));

ENDDOCUMENT();       # This should be the last executable line in the problem. ### Re: Bug in computing powers

by Davide Cervone -
I am not able to reproduce the problem you are having. The MathObject version works fine for me (on a range of values for $n and$k). Can you give the complete problem file and a seed value that fails for you?

Note that in WeBWorK, scientific notation uses an upper-case E, so you must use 1.2E-10 not perl's native 1.2e-10 (which is 1.2 times e minus 10). So it may be that somewhere internally, something is producing perl scientific notation that is then being parsed by MathObjects.

If you can send a seed that fails, perhaps I can track it down. ### Re: Bug in computing powers

by Gabriela Sanchis -
Here is the code, and the seed that is producing a problem is 44.

Thanks.

##DESCRIPTION
##KEYWORDS('probability', 'binomial distribution')
##
## tsch tagged and PAID on 3-22-2004

## DBsubject('Probability')
## DBchapter('Distributions')
## DBsection('Binomial Distribution')
## Date('6/3/2002')
## Author('')
## Institution('')
## TitleText1('Mathematical Statistics')
## EditionText1('6')
## AuthorText1('Wackerly, Mendenhall, Scheaffer')
## Section1('3.4')
## Problem1('27')

##ENDDESCRIPTION

DOCUMENT();        # This should be the first executable line in the problem.

"PG.pl",
"PGbasicmacros.pl",
"PGchoicemacros.pl",
"PGgraphmacros.pl",
"contextIntegerFunctions.pl",
"PGnumericalmacros.pl",
"PGauxiliaryFunctions.pl"
);

TEXT(beginproblem());
$showPartialCorrectAnswers = 1; Context("IntegerFunctions");$n = random(20,30,1);
$k = random(int(.7*$n), int(.9*$n),1);$ans = 0;
for ($i =$k; $i<($n+1); $i++){$ans = $ans + fact($n)/fact($i)/fact($n - $i)/2**$n;
}

$ans=Compute($ans);
BEGIN_TEXT

A man claims to have extrasensory perception. As a test, a fair coin is flipped $$n$$ times, and the man is asked to predict the outcome in
advance. He gets $$k$$ out of $$n$$ correct. What is the probability that he would have done at least this well if he had no ESP? $BR \{ans_rule(20)\} END_TEXT ANS($ans->cmp() );
#ANS(num_cmp($ans)); ENDDOCUMENT(); # This should be the last executable line in the problem. In reply to Gabriela Sanchis ### Re: Bug in computing powers by Davide Cervone - 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.