Documentation:WeBWorK/Problem Authoring Templates/Numeric response

From UBC Wiki
Jump to: navigation, search


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:

  1. In the problem set-up section, create a Real MathObject with the correct value and assign it to a known variable. E.g, $meaning_of_life = Real(42);.
  2. In the answer section, call the ANS command with the correct value's cmp method. E.g., ANS($meaning_of_life->cmp).


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.

See the page on numeric tolerance for more templates, and the MAA wiki for details.

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.

Source code

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.

Template file source.