Difference between revisions of "FormattingDecimals"

From WeBWorK_wiki
Jump to navigation Jump to search
m
m
 
(13 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<h2>Formatting Decimals: PG Code Snippet</h2>
+
<h2>Formatting Decimals and Using Logarithms: PG Code Snippet</h2>
   
 
<!-- Header for these sections -- no modification needed -->
 
<!-- Header for these sections -- no modification needed -->
   
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
 
<p style="background-color:#eeeeee;border:black solid 1px;padding:3px;">
<em>We show how to format decimals for display in PG problems. Note that these are <b>insertions</b>, not a complete PG file. This code will have to be incorporated into the problem file on which you are working.</em>
 
  +
<em>We show how to use format decimals, and, conveniently also how to use logarithmic functions in PG problems.</em>
 
</p>
 
</p>
   
Line 23: Line 23:
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<td style="background-color:#ddffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
loadMacros("PGstandard.pl","MathObjects.pl");
 
  +
DOCUMENT();
  +
  +
loadMacros(
  +
"PGstandard.pl",
  +
"MathObjects.pl"
  +
);
  +
  +
TEXT(beginproblem());
 
</pre>
 
</pre>
 
</td>
 
</td>
Line 39: Line 46:
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<td style="background-color:#ffffdd;border:black 1px dashed;">
 
<pre>
 
<pre>
$a = random(3,7,1);
 
  +
Context("Numeric");
# log is natural log, and ln is also natural log
 
  +
Context()->variables->set(x=>{limits=>[2,4]});
$b = sprintf("%0.3f", log($a)/log(10) );
 
  +
  +
#
  +
# both ln and log are natural log (base e)
  +
#
  +
  +
$a = 6; # or $a = random(3,7,1);
  +
  +
#
  +
# log base e
  +
#
  +
$b = sprintf("%0.3f", ln($a) ); # or log($a)
  +
$solution1 = Real("$b");
  +
  +
$f = Formula("ln(x)"); # or log(x)
  +
$solution2 = $f->eval(x=>$a);
  +
  +
#
  +
# log base 10 is log10, logten,
  +
# ln(x)/ln(10), or log(x)/log(10)
  +
#
  +
$c = sprintf("%0.3f", ln($a)/ln(10) ); # or log($a)/log(10)
  +
$solution3 = Real("$c");
  +
  +
$g = Formula("ln(x)/ln(10)"); # or log(x)/log(10)
  +
$solution4 = $g->eval(x=>$a);
 
</pre>
 
</pre>
 
</td>
 
</td>
 
<td style="background-color:#ffffcc;padding:7px;">
 
<td style="background-color:#ffffcc;padding:7px;">
 
<p>
 
<p>
<b>Setup:</b>
+
<b>Setup:</b>
Use perl's <code>sprintf( format, number );</code> command to format the decimal. The <code>"%0.3f"</code> portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set <code>"%0.2f"</code> for two decimal places and zero filling (for example, <code>sprintf("%0.2f",0.5);</code> returns <code>0.50</code>). You can do a web search for more options to perl's <code>sprintf</code>, and also for WeBWorK's <code>contextCurrency.pl</code>. If you do further calculations with <code>$a</code>, be aware that numerical error may be an issue since you've reduced the number of decimal places.
+
Since the domain of a logarithmic function is all positive real numbers, we should set the domain of function evaluation to <code>[2,4]</code> in order to avoid vertical asymptotes and places where a logarithmic function takes values close to zero.
  +
</p>
  +
<p>
  +
Use perl's <code>sprintf( format, number );</code> command to format the decimal. The <code>"%0.3f"</code> portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set <code>"%0.2f"</code> for two decimal places and zero filling (for example, <code>sprintf("%0.2f",0.5);</code> returns <code>0.50</code>). You can do a web search for more options to perl's <code>sprintf</code>, and also for WeBWorK's <code>contextCurrency.pl</code>. If you do further calculations with <code>$b</code>, be aware that numerical error may be an issue since you've reduced the number of decimal places.
 
</p>
 
</p>
 
<p>
 
<p>
Line 53: Line 84:
 
</p>
 
</p>
 
<p>
 
<p>
Note: If we load <code>MathObjects.pl</code>, then <code>log</code> and <code>ln</code> are both defined to be the natural logarithm (base e, not base 10). If we had loaded the older <code>PGauxiliaryFunctions.pl</code> macro instead, then <code>ln</code> would be undefined and <code>log</code> would be defined as the natural logarithm (base e, not base 10).
+
Note: If we load <code>MathObjects.pl</code>, then <code>log</code> and <code>ln</code> are both defined to be the natural logarithm (base e, not base 10). If we had loaded the older <code>PGauxiliaryFunctions.pl</code> macro instead, then <code>log</code> would be defined as the natural logarithm (base e, not base 10), and <code>ln</code> would be undefined.
  +
</p>
  +
<p>
  +
It is possible to set a context flag that will use the base 10 log via <code>Context()->flags->set(useBaseTenLog=>1);</code> The default is that this is set to zero.
  +
</p>
  +
<p>
  +
If you would like to define log base 2 (or another base) see [http://webwork.maa.org/wiki/AddingFunctions AddingFunctions] for how to define and add a new function to the context so that students can enter it in their answers.
 
</p>
 
</p>
 
</td>
 
</td>
Line 63: Line 94:
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<td style="background-color:#ffdddd;border:black 1px dashed;">
 
<pre>
 
<pre>
  +
Context()->texStrings;
 
BEGIN_TEXT
 
BEGIN_TEXT
   
\( $b = \) \{ ans_rule(20) \}
 
  +
Notice the formatting and rounding differences
  +
between \( $solution1 \) and \( $solution2 \).
  +
$BR
  +
$BR
  +
Try entering \( \ln($a), \log($a),
  +
\ln($a)/\ln(10), \log($a)/\log(10),
  +
\mathrm{logten}($a), \mathrm{log10}($a) \).
  +
$BR
  +
$BR
  +
\( \ln($a) = \) \{ ans_rule(20) \}
  +
$BR
  +
\( \ln($a) = \) \{ ans_rule(20) \}
  +
$BR
  +
\( \log_{10}($a) = \) \{ ans_rule(20) \}
  +
$BR
  +
\( \log_{10}($a) = \) \{ ans_rule(20) \}
   
 
END_TEXT
 
END_TEXT
  +
Context()->normalStrings;
 
</pre>
 
</pre>
 
<td style="background-color:#ffcccc;padding:7px;">
 
<td style="background-color:#ffcccc;padding:7px;">
 
<p>
 
<p>
 
<b>Main Text:</b>
 
<b>Main Text:</b>
Display the formatted number.
 
  +
Notice the difference in decimal formatting when "Show Correct Answers" is checked and you click "Submit Answers".
 
</p>
 
</p>
 
</td>
 
</td>
Line 82: Line 130:
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<td style="background-color:#eeddff;border:black 1px dashed;">
 
<pre>
 
<pre>
ANS( $b->cmp() );
+
ANS( $solution1->cmp() );
  +
ANS( $solution2->cmp() );
  +
ANS( $solution3->cmp() );
  +
ANS( $solution4->cmp() );
  +
  +
ENDDOCUMENT();
 
</pre>
 
</pre>
 
<td style="background-color:#eeccff;padding:7px;">
 
<td style="background-color:#eeccff;padding:7px;">

Latest revision as of 09:40, 7 May 2012

Formatting Decimals and Using Logarithms: PG Code Snippet


We show how to use format decimals, and, conveniently also how to use logarithmic functions in PG problems.

Problem Techniques Index

PG problem file Explanation
DOCUMENT();

loadMacros(
"PGstandard.pl",
"MathObjects.pl"
);

TEXT(beginproblem());

Initialization: Standard.

Context("Numeric");
Context()->variables->set(x=>{limits=>[2,4]});

#
# both ln and log are natural log (base e)
#

$a = 6; # or $a = random(3,7,1);

#
# log base e
#
$b = sprintf("%0.3f", ln($a) ); # or log($a)
$solution1 = Real("$b");

$f = Formula("ln(x)"); # or log(x)
$solution2 = $f->eval(x=>$a);

#
# log base 10 is log10, logten, 
# ln(x)/ln(10), or log(x)/log(10) 
#
$c = sprintf("%0.3f", ln($a)/ln(10) ); # or log($a)/log(10)
$solution3 = Real("$c");

$g = Formula("ln(x)/ln(10)"); # or log(x)/log(10)
$solution4 = $g->eval(x=>$a);

Setup: Since the domain of a logarithmic function is all positive real numbers, we should set the domain of function evaluation to [2,4] in order to avoid vertical asymptotes and places where a logarithmic function takes values close to zero.

Use perl's sprintf( format, number ); command to format the decimal. The "%0.3f" portion truncates after 3 decimal places and uses zeros (not spaces) to right-justify. For answers involving money, you should set "%0.2f" for two decimal places and zero filling (for example, sprintf("%0.2f",0.5); returns 0.50). You can do a web search for more options to perl's sprintf, and also for WeBWorK's contextCurrency.pl. If you do further calculations with $b, be aware that numerical error may be an issue since you've reduced the number of decimal places.

We used the logarithm change of base formula log10(a) = log(a) / log(10) = ln(a) / ln(10) to get a logarithm base 10.

Note: If we load MathObjects.pl, then log and ln are both defined to be the natural logarithm (base e, not base 10). If we had loaded the older PGauxiliaryFunctions.pl macro instead, then log would be defined as the natural logarithm (base e, not base 10), and ln would be undefined.

It is possible to set a context flag that will use the base 10 log via Context()->flags->set(useBaseTenLog=>1); The default is that this is set to zero.

If you would like to define log base 2 (or another base) see AddingFunctions for how to define and add a new function to the context so that students can enter it in their answers.

Context()->texStrings;
BEGIN_TEXT

Notice the formatting and rounding differences 
between \( $solution1 \) and \( $solution2 \).
$BR
$BR
Try entering \( \ln($a), \log($a), 
\ln($a)/\ln(10), \log($a)/\log(10),
\mathrm{logten}($a), \mathrm{log10}($a) \).
$BR
$BR
\( \ln($a) = \) \{ ans_rule(20) \}
$BR
\( \ln($a) = \) \{ ans_rule(20) \}
$BR
\( \log_{10}($a) = \) \{ ans_rule(20) \}
$BR
\( \log_{10}($a) = \) \{ ans_rule(20) \}

END_TEXT
Context()->normalStrings;

Main Text: Notice the difference in decimal formatting when "Show Correct Answers" is checked and you click "Submit Answers".

ANS( $solution1->cmp() );
ANS( $solution2->cmp() );
ANS( $solution3->cmp() );
ANS( $solution4->cmp() );

ENDDOCUMENT();

Answer Evaluation: Standard.

Problem Techniques Index