Note: You are currently viewing documentation for Moodle 2.2. Up-to-date documentation for the latest stable version is available here: Calculated question type.

Calculated question type

From MoodleDocs


Calculated questions offer a way to create individual numerical questions by the use of wildcards (i.e {x} , {y})that are substituted with random values when the quiz is taken.

For example, if you want to create a large number of "Calculate the area of a rectangle" problems to drill your students, you could create a question with two wildcards (i.e. {base}, {height}) and put in the "Correct Answer Formula=" input field {base} * {height} ( * being the multiplication sign ).

Correct Answer Formula= {base}*{height}


When a student takes the test, Moodle will randomly select values for {base} and {height} and grade the response using the result of the Correct Answer Formula=.

The test will very rarely appear the same way twice.

CAUTION: At least ONE wildcard in one of the answers

The main purpose of calculated question is to create multiple versions of a question with different numerical values.

Otherwise use numerical question.

A three steps process

To create (or modify) a calculated question you need:

Define the usual question parameters

Name

Give the question a descriptive name.

Question text

All variables "wildcard" you want Moodle to replace with generated values must be placed in curly braces. For example if you wanted to sum numbers A and B the wild cards would be {A} and {B} or even {FirstNumber} and {SecondNumber}
Question text should contains at least one wildcard (otherwise you wouldn't be using this kind of question).
The user have all the necessary information to answer correctly

Correct answer formula

  • define at least one formula answer containing at least the wildcards that were put in the question text and all the other parameter related to precision or units.
  • set 100% at least to one of the answers

Precise the dataset definitions

For each wild card(s) there is a the dataset containing the data that will be substituted to the wild card.
The dataset can be
  • private i.e. only used by this question
  • shared i.e shared with other questions from the same category
A dataset definition is mandatory for each of the wild cards used in the formulas.
If you put in the question text some text between brackets {zzz} that is a possible wild card ,
you will be able to choose if it is really a wild card or not.

Define at least one set of values for each wild card

If you define a large number of values (actual maximum number = 100),
you get a more randomized set of different questions.

Question set-up

  1. If you wish, add general feedback.
  2. Enter the formula for the answer. Be sure to use the same placeholders so Moodle can substitute the same values.
  3. Determine the tolerance for error that you will accept in the answer. The tolerance and tolerance type combine to give a range of acceptable scores.
  4. Select the number of significant figures you want in the correct answer.
  5. Add correct answer feedback.
  6. Enter the units for the answer (e.g. meters, kg, etc.). Moodle will look for the correct units. If you want to enter other acceptable units, such as metric versus imperial distances, enter them along with a conversion factor.
  7. Click Save changes.
  8. On the next screen, choose whether to create substitution values for each placeholder only for this question, or for other questions in the same category.
  9. Click Save changes.
  10. Create a dataset for the question or questions in the category. For each placeholder, generate a series of acceptable values. The more values you generate, the more a question can be used without repeating values.
  11. Click Back to quiz editing.

Correct answer formula

DO NOT PUT THE = sign in the fomula.

  • In the recent versions of the calculated question type, you could have more than one answer formula and applied a specific grading value to each of them as long as there is at least one 100% correct answer formula.
If more than one correct answer formula input field are displayed when editing, 
your site has the multiple answer feature. 


  • As a general rule, write these formulas like you would in a calculator e.g. 3 + 5 * sin (3/{x})
  • Each function's placeholders and other arguments should be in parentheses (brackets). For example, if you want students to calculate the sine of one angle and two times cosine of another, you would enter sin({a}) + cos({b}*2).
  • It's usually better to have too many parentheses (brackets) than too few. The server won't care, and the more specific you are about what you mean, the more likely it will like your complex formulas.
  • There is no implicit multiplication. To you, the human editor, "5(23)" or "5x" may seem perfectly obvious. To the server doing the math, it's crazy talk and won't be understood. Always use the "*" for multiplication.
  • Any special mathematical function must have parentheses around its values. Take the sine function in the first bullet point for instance. Notice that the 3 / x is wrapped in parentheses (brackets)--this is so the server can understand it properly. Without those parentheses, the server won't know if you mean "(sin 3) / x" or "sin (3 / x)" and will reject the entire formula accordingly.

Available functions

  • Calculated questions can use more than simple arithmetic operators. See below for a table of available functions (taken from PHP: Mathematical Functions, where you can get further functions, though be careful, as the page may contain more information than you want!).
Function Explanation
abs Absolute value
acos Arc cosine -- in radians!!! Convert your degree measurement to radians before you take the acos of it.
acosh Inverse hyperbolic cosine -- in radians!!! Convert your degree measurement to radians before you take the acosh of it.
asin Arc sine -- in radians!!! Convert your degree measurement to radians before you take the asin of it.
asinh Inverse hyperbolic sine -- in radians!!! Convert your degree measurement to radians before you take the asing of it.
atan2 Arc tangent of two variables -- pass in two values like (x, y), and you'll get the atah(y/x), adjusted to the proper quadrant.
atan Arc tangent -- in radians!!! Convert your degree measurement to radians before you take the atan of it.
atanh Inverse hyperbolic tangent
base_convert Convert a number between arbitrary bases. Example: to get the value of 50 (in base 10) in base 5, you'd write "base_convert(50, 10, 5)"
bindec Binary to decimal
ceil Round fractions up
cos Cosine -- in radians!!! Convert your degree measurement to radians before you take the cos of it.
cosh Hyperbolic cosine -- in radians!!! Convert your degree measurement to radians before you take the cosh of it.
decbin Decimal to binary
dechex Decimal to hexadecimal
decoct Decimal to octal
deg2rad Converts the number in degrees to the radian equivalent
exp Calculates the exponent of e
expm1 Returns exp(number) - 1, computed in a way that is accurate even when the value of number is close to zero
floor Round fractions down
fmod Returns the floating point remainder (modulo) of the division of the arguments
hexdec Hexadecimal to decimal
is_finite Finds whether a value is a legal finite number
is_infinite Finds whether a value is infinite
log10 Base-10 logarithm
log1p Returns log(1 + number), computed in a way that is accurate even when the value of number is close to zero
log Natural logarithm (ln)
max Find highest value
min Find lowest value
mt_rand Generate a better random value
octdec Octal to decimal
pi Get value of pi
pow (numberToRaise, NumberRaisedTo) Exponential expression
rad2deg Converts the radian number to the equivalent number in degrees
rand Generate a random integer
round Rounds a float
sin Sine -- in radians!!! Convert your degree measurement to radians before you take the sin of it.
sinh Hyperbolic sine -- in radians!!! Convert your degree measurement to radians before you take the sinh of it.
sqrt Square root
tan Tangent -- in radians!!! Convert your degree measurement to radians before you take the tan of it.
tanh Hyperbolic tangent -- in radians!!! Convert your degree measurement to radians before you take the tanh of it.

Predefined constants

  • Some predefined constants are available for you to use. A table of these appears below, altered from PHP: Mathematical Functions, where you can get more information. (Be careful, as the page may contain more information than you want!)
Constant Value Description
M_PI 3.14159265358979323846 Pi
M_E 2.7182818284590452354 e
M_LOG2E 1.4426950408889634074 log_2 e
M_LOG10E 0.43429448190325182765 log_10 e
M_LN2 0.69314718055994530942 log_e 2
M_LN10 2.30258509299404568402 log_e 10
M_PI_2 1.57079632679489661923 pi/2
M_PI_4 0.78539816339744830962 pi/4
M_1_PI 0.31830988618379067154 1/pi
M_2_PI 0.63661977236758134308 2/pi
M_2_SQRTPI 1.12837916709551257390 2/sqrt(pi)
M_SQRT2 1.41421356237309504880 sqrt(2)
M_SQRT1_2 0.70710678118654752440 1/sqrt(2)

Additional predefined constants

  • Some other constants may be available, depending on how your instance of Moodle is set up.
Constant Value Description
M_SQRTPI 1.77245385090551602729 sqrt(pi)
M_SQRT3 1.73205080756887729352 sqrt(3)
M_LNPI 1.14472988584940017414 log_e(pi)
M_EULER 0.57721566490153286061 Euler constant