## Forum archive 2000-2006

### Thomas Hagedorn - Weird Webwork Square Root Error

by Arnold Pizer -
Number of replies: 0
 Weird Webwork Square Root Error topic started 11/22/2004; 12:41:17 PMlast post 11/22/2004; 9:09:12 PM
 Thomas Hagedorn - Weird Webwork Square Root Error  11/22/2004; 12:41:17 PM (reads: 1069, responses: 4) In Problem setLinearAlgebra17DotProductRn/ur_la_17_13.pg with initial seed 773, one gets the following error. Other seeds work fine and the code looks fine so I'm really confused. The error seems to be coming from a sqrt error in line 31 of the code. I initially thought the sqrts later in the problem were causing the error but even when inserted code to insure that those numbers $norm_x,$norm_y were positive, the problem still didn't work as the error was coming earlier. But there's no square root earlier! I'm baffled. -Tom Software ErrorWeBWorK has encountered a software error while attempting to process this problem. It is likely that there is an error in the problem itself. If you are a student, contact your professor to have the error corrected. If you are a professor, please consut the error output below for more informaiton.Error messagesCan't take sqrt of -4.44089e-16 at (eval 1491073) line 31.Error context Problem1ERROR caught by Translator while processing problem file:setHagedorn/WeirdError.pg*Can't take sqrt of -4.44089e-16 at (eval 1491073) line 31.*------Input Read1 ##DESCRIPTION2 ##KEYWORDS('linear algebra', 'inner product', 'dot product', 'angle')3 ##4 ##ENDDESCRIPTION5 6 DOCUMENT(); # This should be the first executable line in the problem.7 8 loadMacros(9 PG.pl,10 PGbasicmacros.pl,11 PGchoicemacros.pl,12 PGanswermacros.pl,13 PGgraphmacros.pl,14 PGnumericalmacros.pl,15 PGstatisticsmacros.pl,16 PGauxiliaryFunctions.pl,17 PGmatrixmacros.pl18 );19 20 TEXT(beginproblem());21 $showPartialCorrectAnswers = 1;22 23$prod = 0;24 $norm_x = 0;25$norm_y = 0;26 27 foreach $i (0..1) {28$x[$i] = non_zero_random(-5,5,1);29$y[$i] = non_zero_random(-5,5,1);30 if ($y[$i] ==$x[$i]) { 31$y[$i] = 6;32 }33$prod = $prod +$x[$i] *$y[$i];34$norm_x = $norm_x +$x[$i] *$x[$i];35$norm_y = $norm_y +$y[$i] *$y[$i];36 37 }38 39 if($norm_x <0){$norm_x= -$norm_x};40 if($norm_y <0){$norm_y= -$norm_y};41 42$norm_x = sqrt($norm_x);43$norm_y = sqrt($norm_y);44 45$angle = arccos( $prod /$norm_x / $norm_y);46 47 BEGIN_TEXT48 49 Find the angle ( alpha ) between the vectors 50 { mbox( display_matrix([[$x[0]], [$x[1]]]), ' and ', display_matrix([[$y[0]], [$y[1]]]), '.' ) } 51$BR52 ( alpha = ) {ans_rule(20)}.53 54 END_TEXT55 56 ANS(num_cmp($angle));57 ANS($norm_x);58 ANS($norm_y);59 ANS($x[1]);60 ANS($x[0]);61 ANS($y[0]);62 ANS($y[1]);63 64 65 ENDDOCUMENT(); # This should be the last executable line in the problem.--Edit this problemThis set is visible to students. <| Post or View Comments |>  John Jones - Re: Weird Webwork Square Root Error 11/22/2004; 2:17:20 PM (reads: 1269, responses: 0) Hi, The error comes from the arccos call. It is trying to take arccos(-1), but with round off error, it is arccos of something slightly less than -1. The result is not defined, but only shows up in the internal compuation of arccos (which involves a square root). John <| Post or View Comments |>  Arnold K. Pizer - Re: Weird Webwork Square Root Error 11/22/2004; 4:45:12 PM (reads: 1301, responses: 0) Hi, This is now fixed in the CVS. The fix involved replacing a segment of code by $norm_x = sqrt($norm_x);$norm_y = sqrt($norm_y);$value = $prod /$norm_x / $norm_y;if ($value > 1) {$value = 1;}if ($value < -1) {$value = -1;} ## avoid round off errors$angle = arccos( \$value ); <| Post or View Comments |>

 Thomas Hagedorn - Re: Weird Webwork Square Root Error  11/22/2004; 6:44:52 PM (reads: 1279, responses: 0) Thanks for the correction! Did the error message make sense when it says the error took place in line 31? The arccos(x) is in line 45. -Tom <| Post or View Comments |>

 John Jones - Re: Weird Webwork Square Root Error  11/22/2004; 9:09:12 PM (reads: 1237, responses: 0) I saw a slightly different error message, presumably because I tested on a different version of webwork. The error message I got both had a different line number, and it specified that the line number didn't refer to the source file but to PGcommonFunctions, which is where arccos is defined. John <| Post or View Comments |>