# ProblemsWithUnits

## Units in Problems: PG Code Snippet

*This code snippet shows the essential PG code to require student answers that include units with their answers. 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.*

Note that it is possible to incorporate units with both the newer MathObjects and with the "old-style" answer evaluators. The former are more flexible, so we show those here. A second sample with the "old-style" answer evaluators appears below this.

PG problem file | Explanation |
---|---|

loadMacros( "parserNumberWithUnits.pl", "parserFormulaWithUnits.pl", ); |
To make the answer to the problem be a number with units, we need to load the file |

Context()->variables->are(t=>"Real"); $time = NumberWithUnits( "(1/32)(25 + sqrt(25^2 + 32*16)) s"); $height = FormulaWithUnits( "8 + 25t - 16t^2 ft/s"); |
(Note that in this problem we're using time as our variable, so we reset the Context to use a variable other than x.)
In the problem set-up section of the file, we define a numerical answer as a |

BEGIN_TEXT The equation for the height is \(h(t) = \) \{ ans_rule(35) \}. $BR Please include \{ helpLink('units') \} in your answer. $PAR The object hits the ground when \(t = \) \{ ans_rule(35) \} ${BITALIC}(Include units in your answer!)$EITALIC END_TEXT |
The text section of the problem is unchanged, except that it's a good idea to remind students that they are required to include units in their answer. |

ANS( $height->cmp() ); ANS( $time->cmp() ); |
Finally, in the answer and solution section of the file, the MathObjects know how to check units, and will correctly deal with the case where a student enters a correct answer with different units (e.g., m/s instead of ft/s). |

It is also possible to include problems that require units with "old-style" answer checkers. However, the "old-style" checkers *do not deal with Formulas with units*! So only numerical answers with units can be checked if we eschew MathObjects. In any event, the following code snippet illustrates this.

PG problem file | Explanation |
---|---|

$time = "(1/32)(25 + sqrt(25^2 + 32*16))"; |
In this case we don't need to include any additional tagging or description information, nor any additional initialization. We define the numeric value that we're looking for normally (without units) in the set-up section of the problem. |

BEGIN_TEXT The object hits the ground when \(t = \) \{ ans_rule(35) \} ${BITALIC}(Include units in your answer!)$EITALIC END_TEXT |
As before, it's a good idea to remind students that they will be required to enter units when we put the text of the problem in. |

ANS( num_cmp( $time, units=>"s" ) ); |
And we define the answer evaluator with |