Difference between revisions of "NumericalTolerance"

From WeBWorK_wiki
Jump to navigation Jump to search
m
(Deprecate the problem and add a link to a newer version.)
 
(3 intermediate revisions by 3 users not shown)
Line 1: Line 1:
  +
{{historical}}
  +
  +
<p style="font-size: 120%;font-weight:bold">This problem has been replaced with [https://openwebwork.github.io/pg-docs/sample-problems/problem-techniques/NumericalTolerance.html a newer version of this problem]</p>
  +
 
<h2>Numerical Tolerance in Problems: PG Code Snippet</h2>
 
<h2>Numerical Tolerance in Problems: PG Code Snippet</h2>
   
Line 13: Line 17:
 
<table cellspacing="0" cellpadding="2" border="0">
 
<table cellspacing="0" cellpadding="2" border="0">
 
<tr valign="top">
 
<tr valign="top">
<th> PG problem file </th>
+
<th>PG problem file</th>
<th> Explanation </th>
+
<th>Explanation</th>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
 
Context("Numeric");
 
Context("Numeric");
Context()-&gt;flags-&gt;set(tolerance=&gt;0.0001,tolType=&gt;"absolute");
+
Context()-&gt;flags-&gt;set(
  +
tolerance =&gt; 0.0001,
  +
tolType =&gt; "absolute",
  +
);
   
$ans = Compute("1.5708");
+
$ans = Compute(1.5708);
 
</pre>
 
</pre>
 
</td>
 
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
Line 36: Line 44:
 
</p>
 
</p>
 
<pre>
 
<pre>
$ans = Compute("1.5708")-&gt;with(
+
$ans = Compute(1.5708)-&gt;with(
tolType=&gt;'absolute',tolerance=&gt;'.0001');
+
tolType =&gt; 'absolute',
  +
tolerance =&gt; .0001,
  +
);
 
</pre>
 
</pre>
 
</td>
 
</td>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
Line 51: Line 62:
 
END_TEXT
 
END_TEXT
 
</pre>
 
</pre>
  +
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
Line 57: Line 70:
 
</td>
 
</td>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
Line 62: Line 78:
 
ANS( $ans->cmp() );
 
ANS( $ans->cmp() );
 
</pre>
 
</pre>
  +
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
And we check the answer as we expect. Alternatively, it is possible to specify the tolerance parameters as arguments to <code>cmp()</code> as follows.
+
And we check the answer as we expect. Alternatively, it is possible to specify the tolerance parameters as arguments to <code>cmp()</code> as follows. In this case it is not necessary to specify the tolerance in the Context or when defining the MathObject.
 
<pre>
 
<pre>
ANS( $ans->cmp(tolType=&gt;'absolute',tolerance=&gt;'.0001') );
 
  +
ANS($ans->cmp(
  +
tolType =&gt; 'absolute',
  +
tolerance =&gt; .0001,
  +
));
 
</pre>
 
</pre>
 
</p>
 
</p>
 
</td>
 
</td>
 
</tr>
 
</tr>
</table>
 
   
  +
<!--===============================-->
  +
  +
</table>
 
<p>
 
<p>
 
With old-style answer evaluators:
 
With old-style answer evaluators:
Line 79: Line 102:
 
<table cellspacing="0" cellpadding="2" border="0">
 
<table cellspacing="0" cellpadding="2" border="0">
 
<tr valign="top">
 
<tr valign="top">
<th> PG problem file </th>
+
<th>PG problem file</th>
<th> Explanation </th>
+
<th>Explanation</th>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
Line 88: Line 114:
 
</pre>
 
</pre>
 
</td>
 
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
Line 93: Line 120:
 
</p>
 
</p>
 
</td>
 
</td>
  +
  +
<!--===============================-->
  +
 
</tr>
 
</tr>
 
<tr valign="top">
 
<tr valign="top">
Line 104: Line 134:
 
END_TEXT
 
END_TEXT
 
</pre>
 
</pre>
  +
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
Line 110: Line 142:
 
</td>
 
</td>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
<tr valign="top">
 
<tr valign="top">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( num_cmp( $ans, tol=&gt;0.0001,
+
ANS(num_cmp($ans,
tolType=&gt;"absolute" );
+
tol =&gt; 0.0001,
  +
tolType =&gt; "absolute",
  +
));
 
</pre>
 
</pre>
  +
</td>
  +
<!------------------------------------------->
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">
 
<p>
 
<p>
Line 122: Line 159:
 
</td>
 
</td>
 
</tr>
 
</tr>
  +
  +
<!--===============================-->
  +
 
</table>
 
</table>
  +
 
<p style="text-align:center;">
 
<p style="text-align:center;">
 
[[IndexOfProblemTechniques|Problem Techniques Index]]
 
[[IndexOfProblemTechniques|Problem Techniques Index]]

Latest revision as of 15:40, 20 June 2023

This article has been retained as a historical document. It is not up-to-date and the formatting may be lacking. Use the information herein with caution.

This problem has been replaced with a newer version of this problem

Numerical Tolerance in Problems: PG Code Snippet

This code snippet shows the essential PG code to specify a numerical tolerance for student answers to a problem. Note that these are insertions, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.

We can control the numerical tolerance expected of the student with MathObjects, or with old-style answer evaluators. Using MathObjects we can control tolerance by setting a "global" tolerance in the Context, as shown in the example below, or by specifying the tolerance that is used for specific MathObjects, as is indicated in the comments to the right. An example with the old-style evaluators appears below the MathObjects examples.

Problem Techniques Index

PG problem file Explanation
Context("Numeric");
Context()->flags->set(
  tolerance => 0.0001,
  tolType => "absolute",
);

$ans = Compute(1.5708);

We need no changes to the tagging and description or initialization sections of the file. In the set-up section, we specify the tolerance flags in the Context. The tolType can be absolute (specifying a decimal distance from the correct answer that will be allowed) or relative (specifying a percent error that will be allowed).

Thus if the correct answer is 17, a tolerance of 0.01 will mean that the student answer must be in the interval (16.99,17.01) if the tolType is absolute, and in the interval (16.83,17.17) if tolType is relative (or omitted, as relative tolerance is the default).

Note that it is also possible to specify the tolerance for the evaluation in the definition of the answer, thereby setting the tolerance only for comparisons using that object. This is done by omitting the Context()->flags code and defining $ans with the following:

$ans = Compute(1.5708)->with(
  tolType => 'absolute',
  tolerance => .0001,
);
BEGIN_TEXT
Enter \(\frac{\pi}{2}\): \{ans_rule(8)\}
$BR
${BITALIC}(Enter your answer accurate to four 
decimal places.)$EITALIC
END_TEXT

The text section of the file is the same as usual.

ANS( $ans->cmp() );

And we check the answer as we expect. Alternatively, it is possible to specify the tolerance parameters as arguments to cmp() as follows. In this case it is not necessary to specify the tolerance in the Context or when defining the MathObject.

ANS($ans->cmp(
  tolType => 'absolute',
  tolerance => .0001,
));

With old-style answer evaluators:

PG problem file Explanation
  $ans = "1.5708";

We need no changes to the tagging and description or initialization sections of the file. In the set-up section, we can specify the value that we're checking against.

  BEGIN_TEXT
  Enter \(\frac{\pi}{2}\): \{ans_rule(8)\}
  $BR
  ${BITALIC}(Enter your answer accurate to four 
  decimal places.)$EITALIC
  END_TEXT

The text section of the file is the same as usual.

ANS(num_cmp($ans,
     tol => 0.0001, 
     tolType => "absolute",
));

And we check the answer, specifying the tolerances we intend in the answer evaluator. The tol and tolType flags behave the same as in the MathObjects version of the problem.

Problem Techniques Index