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