Formulas: More examples

Revision as of 08:24, 2 January 2018 by Dominique Bauer (talk | contribs) (Factorial)

Jump to: navigation, search

Note: You are currently viewing documentation for Moodle 3.3. Up-to-date documentation for the latest stable version of Moodle is probably available here: Formulas: More examples.

Significant figures

"The significant figures of a number are digits that carry meaning contributing to its measurement resolution." (Reference:

The Formulas question has no built-in function to display numbers with a given number of significant figures. However, it is easy to work out this function as the following variable assignment which works with any real number:


where x is the number to be rounded, nsf the number of significant digits to keep and xr the resulting rounded value.


This example deals with the display of the correct answers with different numbers of significant figures.

The Formulas question should look like this:


Play it   Login info  (Open in new tab: Ctrl+Shift+Click)


This example is a significant figures drill.

  Question name!       Significant figures drill
  Random variables     # a     plus or minus sign
                       # b     exponent varying from -4 to 4 in steps of 0.01
                       # rnsf  random number of significant figures : 1, 2, 3, or 4
  Global variables     # nx    number x = ± 10^b
                       # Rounding routine:
                       # x     number to round
                       # nsf   number of significant figures
                       # xr    rounded value of x
Main question
  Question text!       Significant figures drill
Part 1
  Part's mark*         1
  Answer type          Number
  Answer*              xr
  Grading criteria*    Relative error < 0.0001
  Part's text          Number to round: {nx}
                       Number of significant digits to keep: {ncs}
                       Rounded number: {_0}
Combined feedback
  For any correct response
                       The correct answer is: {xr} 
  For any incorrect response
                       The correct answer is: {xr} 

In the Variables fields (random, global and local), lines starting with # are treated as comments.

It is always a good idea to document your work using comments in the Variable fields.

In this example, values to be rounded range from -10 000 to +10 000. In order to have the same number of values in each order of magnitude, hence a more interesting exercise, the random variable b is used as a power of 10. By letting:

x = ± 10b

with b ranging from -4 to +4 in steps of 0.01, there are one hundred random values in each order of magnitude (0.000 1 to 0.001, 0.001 to 0.01, 0.01 to 0.1, 0.1 to 1, 1 to 10, 10 to 100, 100 to 1 000 and 1 000 to 10 000), both positive and negative.

In total, this example generates 6 408 different random questions:

2 (a={-1,1}) × 801 (b={-4:4.01:0.01}) × 4 (rnsf={1,2,3,4}) = 6 408

The Formulas question should look like this:


Play it   Login info  (Open in new tab: Ctrl+Shift+Click)


"In mathematics, the factorial of a non-negative integer n, denoted by n!, is the product of all positive integers less than or equal to n. For example,


The value of 0! is 1, according to the convention for an empty product." (Reference:

With the function fact(), the Formulas question calculates factorial values up to 170!=7.26\rm{E}306 (which would correspond to the largest number that can be handled by PHP). For larger values of n, fact() returns INF for the value of n!.

However, input (with {_0}, {_1}, etc.) is limited to a maximum value corresponding to 102!=9.61\rm{E}161.(Explain why)


This example shows that calculations on factorials can be readily done with the fact() function.

The Formulas question should look like this:


Play it   Login info  (Open in new tab: Ctrl+Shift+Click)

Back to top of page