Documentation:WeBWorK/Problem Authoring Templates/Numeric response
Your problem has a single correct answer that is a scalar and that the student enters into a text field.
There are two parts to your code:
- In the problem set-up section, create a
RealMathObject with the correct value and assign it to a known variable. E.g,
$meaning_of_life = Real(42);.
- In the answer section, call the
ANScommand with the correct value's
The default setting accepts as correct student answers that differ from the correct value by 0.1 percent. If this isn't what you want, you will need to set an alternative value for the answer checker's tolerance. For example,
ANS($meaning_of_life->cmp(tolerance => 0)), but in that case be careful of rounding and numerical accuracy issues.
Take care if the final answer involves the use of ceil() function. Some numbers (like 0.33) have an exact decimal form, but cannot be represented exactly in the binary floating point that Perl uses (just like 1/3 has no exact decimal representation). As a result, floating point numbers may differ slightly from what is expected. It is recommended to round before using that function to avoid errors. It is also suggested to code all of the scientific computing in R rather than in Perl. See integrating WeBWorK with R for more information.
Please download the following attachment to access the code in a single file. Unfortunately, the Wiki restricts filename extensions, so that it couldn't end in ".pg", which is what WebWork expects. You can rename it after downloading, or simply copy-and-paste its contents into the standard WebWork browser-based problem editor.