PREP 2014 Question Authoring - Archived

round-off error and rounding

round-off error and rounding

by Gavin LaRose -
Number of replies: 1

Hi all,

This example of the issue Davide pointed out relative to this post involving the round-off error associated with decimals came up in a problem that I was working on, so I'm putting it here as another example. Consider the problem text

$p1 = 0.1;  $i = 0;
while ( round(10*$p1) != 9 && $i < 10) {
    $p1 = 0.5 + non_zero_random(-0.2,0.4,0.1);
    $i++;
}
$p2 = 0.9 - $p1;
BEGIN_TEXT
$p1, $p2
END_TEXT

(Note that the loop is to ensure that the value of $p1 is 0.9, and that this was generated through the call to non_zero_random. I included the counter $i because I really don't like having loops that don't terminate.)

The output is 0.9, -1.11022302462516e-16, which suggests to me that we're getting numeric (data storage and/or round off) error when calculating the values.

It is interesting to try this and the (theoretically equivalent, but not)

$p1 = 0.9;
$p2 = 0.9 - $p1;
BEGIN_TEXT
$p1, $p2
END_TEXT

In Davide's interactive problem lab.

Gavin

In reply to Gavin LaRose

Re: round-off error and rounding

by Genevieve Toutain -
This seemed to be the closest to the issue I am having, so I thought I would reply here to bump it to the top.  I am authoring a question that includes a random one decimal between -10 and 10, hence:

$a = random(-10,10,.1);


I just happened to have seed 1612, and when printing out $a in the problem, instead of printing -0.02 (as I suspect it wanted to), it is printing out -0.1999999999. 


Any ideas on what I should do here?  Is there a way to restrict how many decimal places WeBWork displays?  Is this a known issue?  I tried searching the forums to not much avail. 


I've gotten around this by replacing it with 


$a = random(-10,10)+random(0,1,.1);


which while technically not the same serves my purposes just fine.  I'm a little concerned there are other lurking decimal issues.