WeBWorK Problems

significant digits

significant digits

by Carl Yao -
Number of replies: 5
Hi:

I'm trying to author a problem which asks a student to enter the answer in a random number of significant digits. However, it seems WeBWorK allows some wrong answers. For example, if the correct answer is 12.34, WeBWorK accepts 12.33 and 12.35 as correct. Is there a quick way to fix this?

Thank you!

Carl Yao


DOCUMENT();

############################################################
# Load Macros
############################################################
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"PGcourse.pl",
);

############################################################
# Header
############################################################

#COMMENT('');
TEXT(beginproblem());

############################################################
# PG Setup
############################################################
Context("Numeric");

do {
$num1 = random(10,100,0.01);
$num2 = random(10,100,0.01);
$num3 = random(10,100,0.01);
$num4 = random(10,100,0.01);
$N = ($num1-$num2)/($num3-$num4);
} until ( $N>0 );

$b = $N;
$c = floor($b);

## Counting the no. of digits
## to the left of decimal point
## in the given no.
for ($i = 0; $b >= 1; ++$i){
$b = $b / 10;}

$n = ($i-2)>1 ? random($i-2,$i+3,1) : random(1,$i+3,1);
$d = $n - $i;
$b = $N;
$b = $b*10**$d;
$e = $b + 0.5;
if ($e == ceil($b)) {
$f = (ceil($b));
$h = $f - 2;
if ($h % 2 != 0) {
$e = $e - 1;
}
}
$j = floor($e);
$m = 10**$d;
$ans = $j / $m;

############################################################
# Body
############################################################

BEGIN_PGML

Calculate [`\frac{[$num1]-[$num2]}{[$num3]-[$num4]}`]

Give your answer correct to [`[$n]`] significant figures.

The answer is [______________]{$ans}.

END_PGML

############################################################
# Solution
############################################################

#BEGIN_PGML_SOLUTION


#END_PGML_SOLUTION

############################################################
# End Problem
############################################################

ENDDOCUMENT();
In reply to Carl Yao

Re: significant digits

by Ryan Maccombs -
Take a look at

http://webwork.maa.org/wiki/NumericalTolerance#.W2RyD9ApB-E

I think that should answer your question

In reply to Ryan Maccombs

Re: significant digits

by Carl Yao -
Thank you Ryan! I looked at that article before, but I never thought of setting tolerance to 0. That solved my problem.

Carl Yao
In reply to Carl Yao

Re: significant digits

by Alex Jordan -
The default is to use relative tolerance of 0.1%. So if the real answer is 12.34, then any answer between 12.34/(1.001) and 12.34*(1.001) is correct. That's 12.327... and 12.352...

You need to change the tolType flag to absolute. And then you need to set the value of tolerance according to the number of significant digits and the order of magnitude of your random number. With 12.23, tolerance=>0.001 would work. With 122.3, tolerance=>0.01 would work.

Of course if you are insisting that something actually get rounded by the student, you need to ask for exact answers. (Like if you are asked to round 12.2301, are you trying to prevent "12.2301" from being accepted?) For this, in theory either tolType would work with tolerance=>0. However I'm not sure that tolerance=>0 would work in practice. You could try tolerance=>0.000000001 or something like that. Or maybe someone knows of another solution.

Some future day, there could be a "sigfig" tolType that would help here. But it would be a nontrivial project.
In reply to Alex Jordan

Re: significant digits

by Carl Yao -
The trick of tolerance=>0 worked. Thanks!

Carl Yao

In reply to Carl Yao

Re: significant digits

by Alex Jordan -
OK, but be careful about tolerance=>0. At a minimum, only use it this way if you explicitly define the answer to be a terminating decimal. If you defined $ans to be something like "0.2/0.4", then in binary with machine rounding that might be "0.001100/0.011001" (with the rounding happening much further down the line) which is only 0.48 in decimal.

So in this example, you might be sure that you are defining the answer to be 0.5, but it comes out as something different. Usually the tolerance being greater than 0 absorbs this issue. But with tolerance=>0, the student would type in 0.5 and be marked wrong.