WeBWorK Problems

Precision of numerical results, and Why are decimal student answers rounded?

Precision of numerical results, and Why are decimal student answers rounded?

by Paul Seeburger -
Number of replies: 4

I am still a little confused about how to get numerical results to display with the number of decimal places of accuracy that I desire in WebWork.

For example, I use the following code in a midpoint sum integral approximation problem I am editing to meet my needs:

Context("Numeric");
Context()->flags->set(
  reduceConstantFunctions=>1, # combine 4+5*2?
);

$a = 0;
$b = Real(random(1, 3));

$c = Real(random(2, 5, 1));

$df = Formula("x ** $c");
$f = Formula("$df ** 2");

$k2 = 1;

$N = 4; ##random(3, 5);

$dx = Formula("($b - $a) / $N")->reduce();

$m_ans = Formula("0");

for ($j = 1; $j <= $N; $j++)
{
    $m_ans += $f->eval(x=>($a + ($j - (1.0/2.0)) * $dx->eval()));
}
$m_ans *= $dx;

@m = ();
for ($i = 1; $i <= $N; $i++)
{
    $m[$i - 1] = $a + $dx->eval() * ($i - (1/2));
}

@m_list = ();
for ($i = 0; $i < $N; $i++)
{
    $m_list[$i] = $f->substitute(x=>$m[$i]);
}

Context()->texStrings;
BEGIN_TEXT
$PAR
Use the sum of the volumes of $N disks to calculate an approximation to the volume of the solid obtained by rotating y = \( $df \) about the x-axis over the interval \( \left[ 0, $b \right] \).
$PAR
Use a Midpoint Sum, with N = $N.  Give the answer accurate to 4 decimal places.
$PAR
\( V \approx \) \{ans_rule()\}

END_TEXT
Context()->normalStrings;

$m_ans *= pi;
$m_ans2 = Compute($m_ans);


#$m_ans = Compute("$m_ans*pi*$dx")->with(
#  tolType => 'absolute',
#  tolerance => .0001,
#);
ANS($m_ans->cmp(tolType => 'absolute',
  tolerance => .00001));


Context()->texStrings;
SOLUTION(EV3(<<'END_SOLUTION'));
$PAR
$SOL
$PAR
Using the disk method, the volume is given by
$PAR
\( V = \int^{$b}_{$a}{\pi [R(x)]^2 \, dx} = \pi \int^{$b}_{$a}{{\left( $df \right)}^2 \, dx} \)
$PAR
which can be estimated as \( \pi \left[ MIDPT($N) \right] \).
$PAR
Let \( f(x) = x^{\{$c * 2\}}\).  We divide [$a, $b] into $N subintervals of width
$PAR
\( \Delta x = \frac{$b - $a}{$N} = $dx \)
$PAR
with midpoints \( c_i \) = \{join(", ", @m)\}.  With this data, we get
$PAR
\( V \approx \pi \cdotp MIDPT($N) = \pi \left[ \Delta x \left( f(c_1) + ... + f(c_N) \right) \right]\) $BR
\(= \pi \left[ $dx \left( \{ join(" + ", @m_list) \} \right) \right] \approx $m_ans2 \quad \text{units}^3\)
END_SOLUTION

The red code highlights the variable I am trying to get to be rounded to 5 decimal places (e.g. 51.89886).  No matter how many different ways I have tried to set the tolerance to be .00001 or .000001, the answer and also the student's entered answer always seem to display rounded to only 4 decimal places, e.g., 51.8989 for example, even when the answer is correctly requiring more decimal places of accuracy to be counted correct.  And the student answer is displayed in Preview with only 4 decimal places of accuracy even when they enter more decimal places.

I tried setting the tolerance in the context and also for each evaluation involving $m_ans, but did not seem to change this at all.

Can anyone help me figure out what is limiting the accuracy of my computed result and also the reason the student's answer is not displayed back with the same number of decimal places as it was entered with?

Thanks!

Paul

In reply to Paul Seeburger

Re: Precision of numerical results, and Why are decimal student answers rounded?

by Davide Cervone -
The tolerance value only affects the comparison of numbers for equality; it doesn't affect the display. The display of numbers if controlled by the Context's number format, which is stored in the Context()->{format}{number} string. It is a printf-style string that indicates the type of output desired for the numbers. The default is %g, which is a 6-digit number that will be put into scientific notation if needed for large or small numbers. You can specify another number of digits by using, e.g.,
    Context()->{format}{number} = "%.8g";
to get 8-digit numbers. Note that this is the total number of signification digits, not the number of decimal places. The %g format is a floating-point format.

It sounds like you might prefer to use the fix-point format, which is %f. If you want 5 decimal places, then use

    Context()->{format}{number} = "%.5f";
This does mean decimal places, not total significant digits as with %g, and it will include any trailing zeros, (these are removed in %g output).

See the Wiki page on advanced context modifications for more details.

In reply to Davide Cervone

Re: Precision of numerical results, and Why are decimal student answers rounded?

by Paul Seeburger -

Awesome!

Thanks, Davide!

So there is no standard format yet that allows for both rounding to a particular decimal place and removing trailing zeros?

I suppose just setting more significant digits may be enough if there is a round function, or I could create one of my own.

If I create a new function like this that I want to use in multiple problems, would I place this in a macro in my macros folder (as a user of someone else's WebWork server)?

Thanks again, Davide!

Paul

In reply to Paul Seeburger

Re: Precision of numerical results, and Why are decimal student answers rounded?

by Davide Cervone -
The %.5f# format would remove trailing zeros, as documented in the link at the bottom of my previous message. This is a WW extension to the printf-style format.

I'm sorry, I don't know what "a new function like this" refers to. But yes, if you create code that you want to use multiple times, you can put that in a macro file in your course's templates/macros folder. If you ever contribute your problems to the Open Problem Library (aka National Problem Library), you will need to include your macro files, too.
In reply to Davide Cervone

Re: Precision of numerical results, and Why are decimal student answers rounded?

by Paul Seeburger -

Thanks, Davide!

The function I was referring to was that I had mentioned that I could create a function to round off at a specified number of digits.

I will begin to play with macros some.  That looks powerful!

Again I greatly appreciate your expertise, Davide!

Paul