WeBWorK Problems

non_zero_random displays unexpected output- too many decimals

non_zero_random displays unexpected output- too many decimals

by Tim Alderson -
Number of replies: 2

Tested on WW 2.16 and WW 2.17.

An instructor asked me about an issue with the question below from a live assignment with seed 3463 where $c (which should have at most two decimal places) is displayed as -0.780000000000001.

Is such an error type to be expected once in a while?






DOCUMENT();
loadMacros(
  "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "PGcourse.pl",
);

Context("Numeric");
$showPartialCorrectAnswers = 1;
$a=non_zero_random(-20,20,0.01);
$b=non_zero_random(-20,20,0.01);
$c=non_zero_random(-20,20,0.01);
$mag=sqrt($a**2+$b**2+$c**2);
$smag=sprintf("%.6f",$mag);
@dir=($a/$mag,$b/$mag,$c/$mag);
@sdir=(sprintf("%.6f",$dir[0]),sprintf("%.6f",$dir[1]),sprintf("%.6f",$dir[2]));

TEXT(beginproblem());
BEGIN_PGML
What is the unit vector for the vector [`\langle [$a],[$b],[$c] \rangle`]?

Answer: [`\langle`][__________]{$dir[0]},[__________]{$dir[1]},[__________]{$dir[2]}[`\rangle`]
END_PGML

ENDDOCUMENT();


In reply to Tim Alderson

Re: non_zero_random displays unexpected output- too many decimals

by Alex Jordan -

It's less of an "error" and just a fact of life with machine rounding errors when converting back and forth between decimal and binary, and having to truncate or round non-terminating expressions at some point.

You can mitigate this by using MathObjects, or by randomizing with integers.

Method 1:  Make $c a MathObject:
$c = Real(non_zero_random(-20,20,0.01));
$c still has the excess precision with all the 0s and the 1, but when it is displayed, the Context will oly display it with 6 (a customizable default) significant figures.

Method 2: Randomize using integers, then divide by 100:
$c = non_zero_random(-2000,2000,1)/100;
The machine rounding is less likely to be an issue. I'm not 100% sure that machine rounding won't still be an issue though, and I'd recommend Method 1.



In reply to Alex Jordan

Re: non_zero_random displays unexpected output- too many decimals

by Danny Glin -

There is nothing stopping you from doing both.  In fact I would go a step further and stick with integer arithmetic as far as possible.  For example:

$a1 = non_zero_random(-2000,2000,1);
$b1 = non_zero_random(-2000,2000,1);
$c1 = non_zero_random(-2000,2000,1);

$mag1 = sqrt($a1**2+$b1**2+$c1**2);
@dir = ($a1/$mag1,$b1/$mag1,$c1/$mag1);

$a = $a1/100;
$b = $b1/100;
$c = $c1/100;
$mag = $mag1/100;

On an unrelated note, you can save a lot of effort in this problem by using the MathObject Vector class:

DOCUMENT();
loadMacros(   "PGstandard.pl",
  "MathObjects.pl",
  "PGML.pl",
  "PGcourse.pl",
);

Context("Vector");

$showPartialCorrectAnswers = 1;

$v = Vector([random(-2000,2000,1),random(-2000,2000,1),random(-2000,2000,1)]);
$v1 = $v/100;
$u = $v/norm($v);

TEXT(beginproblem());

BEGIN_PGML
What is the unit vector for the vector [`[$v1]`]?
Answer: [_]*{$u}
END_PGML

ENDDOCUMENT();