-
Notifications
You must be signed in to change notification settings - Fork 0
InexactValueWithUnits problem tutorial
InexactValueWithUnits
is a derived version of InexactValue
. There are many similarities and some differences. It's almost better to think of InexactValueWithUnits
as containing an InexactValue
and a unit. You can use this macro to force students to enter the units in the answer blanks along with the values. If you don't need students to enter units, you can just use InexactValue
. However, this macro can still be useful for creating detailed solutions.
You will need to add the "contextInexactValueWithUnits.pl" macro at the very least in addition to the standard macros:
DOCUMENT();
loadMacros(
"PGstandard.pl",
"MathObjects.pl",
"PGML.pl",
"contextInexactValue.pl",
"contextInexactValueWithUnits.pl"
);
Older versions also need the contextInexactValue.pl
macro. It won't be needed for current versions, but it doesn't hurt.
Start with declaring the InexactValueWithUnits context. You can follow up with several options that determine how the problem will be graded. These are similar to InexactValue with a few additions. If you don't need anything different from the defaults, then you don't need to make these flag set calls!
Context("InexactValueWithUnits");
Context()->flags->set(failOnValueWrong=>1); # must have value correct (ignore all other credit options and make 0% credit)
Context()->flags->set(creditValue=>0.50); # 50% credit for correct answer (default is 50%)
Context()->flags->set(creditSigFigs=>0.25); # 25% credit for sig figs (default is 25%)
Context()->flags->set(creditUnits=>0.25); # 25% credit for units (default is 25%)
Next, start generating your values for your problems using the shortcut InexactValueWithUnits
. While there are two ways to use it, this page will only describe one way (the easiest, least problematic way). The first parameter is always the value. The second parameter is the unit.
$mass = InexactValueWithUnits('2.40', 'g');
$volume = InexactValueWithUnits('3.5', 'mL');
You can make this problem algorithmic by following the example on this wiki page.
See the section on Units below for more details on what can go in the second parameter.
You can do math with InexactValueWithUnits
, too! To get the density, simply divide the two variables:
$density = $mass / $volume;
Now, we can write the PGML question. Notice how there are no units in the question, nor appended after the answer blank.
BEGIN_PGML
Determine the density of an unknown liquid that has a mass of [$mass] and a volume of [$volume].
[______]{$density}
Enter the value with the appropriate number of significant figures and **units**.
END_PGML
The units will be displayed in plain text or in LaTeX format depending on what you are requesting from WeBWorK. If you find the units look ugly in plain text mode (often the case for compound units), you can display them in LaTeX using the an extra set of square braces and backticks: [`[$mass]`]
Units are handled by functions inside both contextInexactValueWithUnits.pl
and betterUnits.pl
. They can be:
- abbreviations
- full names (for non-compound, 1-dimensional units only, but prefixes are ok)*
- prefixed
- with powers (using LaTeX notation)
- compound
- with spaces
- with explicit * or / symbols
*If a full name doesn't work, please submit a pull request and modify 'betterUnits.pl' as needed.
This is true both for writing the problems and for the students entering answers in the blanks.
For example, the fundamental units making a Joule would be written kg m^2 / s^2
or kg*m^2/s^2
or kg m^2 s^-2
. In the above example, mL
could be replaced with milliliters
and g
with grams
.