Difference between revisions of "FormattingDecimals"
Line 47: | Line 47: | ||
<pre> |
<pre> |
||
Context("Numeric"); |
Context("Numeric"); |
||
− | Context() |
+ | Context()->variables->set(x=>{limits=>[2,4]}); |
# |
# |
||
Line 77: | Line 77: | ||
<td style="background-color:#ffffcc;padding:7px;"> |
<td style="background-color:#ffffcc;padding:7px;"> |
||
<p> |
<p> |
||
− | <b>Setup:</b> |
+ | <b>Setup:</b> |
+ | Since the domain of logarithmic functions are positive reals, we should set the domain of function evaluation to something like <code>[2,4]</code> which avoids vertical asymptotes and places where the 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. |
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> |
Revision as of 16:03, 25 April 2010
Formatting Decimals: PG Code Snippet
We show how to format decimals for display in PG problems.
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 logarithmic functions are positive reals, we should set the domain of function evaluation to something like
Use perl's We used the logarithm change of base formula log_{10}(a) = log(a) / log(10) = ln(a) / ln(10) to get a logarithm base 10.
Note: If we load 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. |