Difference between revisions of "NumericalTolerance"
m |
m |
||
Line 19: | Line 19: | ||
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
<td style="background-color:#ffffdd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | Context("Numeric"); |
|
− | + | Context()->flags->set(tolerance=>0.0001,tolType=>"absolute"); |
|
− | tolerance => 0.0001, |
||
− | tolType => "absolute" |
||
− | ); |
||
− | + | $ans = Compute("1.5708"); |
|
</pre> |
</pre> |
||
</td> |
</td> |
||
Line 36: | Line 36: | ||
</p> |
</p> |
||
<pre> |
<pre> |
||
− | + | $ans = Compute("1.5708")->with(tolType=>'absolute',tolerance=>'.0001'); |
|
− | tolType=>'absolute', tolerance=>'.0001'); |
||
</pre> |
</pre> |
||
</td> |
</td> |
||
Line 43: | Line 43: | ||
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
<td style="background-color:#ffdddd;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | BEGIN_TEXT |
|
− | + | Enter \(\frac{\pi}{2}\): \{ans_rule(8)\} |
|
− | + | $BR |
|
− | + | ${BITALIC}(Enter your answer accurate to four |
|
− | + | decimal places.)$EITALIC |
|
− | + | END_TEXT |
|
</pre> |
</pre> |
||
<td style="background-color:#ffcccc;padding:7px;"> |
<td style="background-color:#ffcccc;padding:7px;"> |
||
Line 59: | Line 59: | ||
<td style="background-color:#eeddff;border:black 1px dashed;"> |
<td style="background-color:#eeddff;border:black 1px dashed;"> |
||
<pre> |
<pre> |
||
− | + | ANS( $ans->cmp() ); |
|
</pre> |
</pre> |
||
<td style="background-color:#eeccff;padding:7px;"> |
<td style="background-color:#eeccff;padding:7px;"> |
||
<p> |
<p> |
||
− | And we check the answer as we expect. |
||
+ | 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. |
||
+ | <pre> |
||
+ | ANS( $ans->cmp(tolType=>'absolute',tolerance=>'.0001') ); |
||
+ | </pre> |
||
</p> |
</p> |
||
</td> |
</td> |
Revision as of 20:57, 20 January 2010
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.
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
Thus if the correct answer is 17, a
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 $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 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 |