FormattingDecimals: Difference between revisions
| mNo edit summary | mNo edit summary | ||
| 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); | # | ||
| # log  | # both ln and log are natural log (base e) | ||
| $b = sprintf("%0.3f", log($a)/log(10) ); | # | ||
| $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 | |||
| # | |||
| $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> | ||
| Line 53: | Line 80: | ||
| </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  | 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> | ||
| </td> | </td> | ||
| Line 63: | Line 90: | ||
| <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 | ||
| \( $ | 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) \). | |||
| $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> | ||
| Notice the difference in decimal formatting when "Show Correct Answers" is checked and you click "Submit Answers". | |||
| </p> | </p> | ||
| </td> | </td> | ||
| Line 82: | Line 123: | ||
| <td style="background-color:#eeddff;border:black 1px dashed;"> | <td style="background-color:#eeddff;border:black 1px dashed;"> | ||
| <pre> | <pre> | ||
| ANS( $ | 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;"> | ||
Revision as of 20:30, 16 January 2010
Formatting Decimals: PG Code Snippet
We show how to format decimals for display in PG problems. 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.
| PG problem file | Explanation | 
|---|---|
| DOCUMENT(); loadMacros( "PGstandard.pl", "MathObjects.pl" ); TEXT(beginproblem()); | Initialization: Standard. | 
| #
# 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
#
$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: 
Use perl's  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  | 
| 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) \).
$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. |