If you find this page useful and would like to be notified of changes made to this page, start by inputting your email below.
powered by ChangeDetection
This is the documentation page for Module:Math
This Lua module is used on 1,000,000+ pages. To avoid large-scale disruption and unnecessary server load, any changes to this module should first be tested in its /sandbox or /testcases subpages. The tested changes can then be added to this page in one single edit. Please consider discussing any changes on the talk page before implementing them. |
40x40px | This module is subject to page protection. It is a highly visible module in use by a very large number of pages, or is substituted very frequently. Because vandalism or mistakes would affect many pages, and even trivial editing might cause substantial load on the servers, it is protected from editing. |
This module provides a number of mathematical functions. These functions can be used from #invoke or from other Lua modules.
To use the module from normal wiki pages, no special preparation is needed. If you are using the module from another Lua module, first you need to load it, like this:
<source lang="lua"> local mm = require('Module:Math') </source>
(The mm
variable stands for Module Math; you can choose something more descriptive if you prefer.)
Most functions in the module have a version for Lua and a version for #invoke. It is possible to use the #invoke functions from other Lua modules, but using the Lua functions has the advantage that you do not need to access a Lua frame object. Lua functions are preceded by _
, whereas #invoke functions are not.
{{#invoke:math|random}} {{#invoke:math|random|max_value}} {{#invoke:math|random|min_value|max_value}}
<source lang="lua"> mm._random() mm._random(max_value) mm._random(min_value, max_value) </source>
Generates a random number.
This function will not work properly for numbers less than -2^32 and greater than 2^32 - 1 (although this may vary by hardware). If you need to use numbers outside of this range, it is recommended that you use Module:Random.
{{#invoke:math|order|n}}
<source lang="lua"> mm._order(n) </source>
Determines the order of magnitude of a number.
{{#invoke:math|precision|n}} {{#invoke:math|precision|x=n}}
<source lang="lua"> mm._precision(number_string) </source>
Detemines the precision of a number. For example, for "4" it will return "0", for "4.567" it will return "3", and for "100" it will return "-2".
The function attempts to parse the string representation of the number, and detects whether the number uses E notation. For this reason, when called from Lua, very large numbers or very precise numbers should be directly input as strings to get accurate results. If they are input as numbers, the Lua interpreter will change them to E notation and this function will return the precision of the E notation rather than that of the original number. This is not a problem when the number is called from #invoke, as all input from #invoke is in string format.
{{#invoke:math|max|v1|v2|v3|...}}
<source lang="lua"> mm._max(v1, v2, v3, ...) </source>
Returns the maximum value from the values specified. Values that cannot be converted to numbers are ignored.
{{#invoke:math|min|v1|v2|v3|...}}
<source lang="lua"> mm._min(v1, v2, v3, ...) </source>
Returns the minimum value from the values specified. Values that cannot be converted to numbers are ignored.
{{#invoke:math|average|v1|v2|v3|...}}
<source lang="lua"> mm._average(v1, v2, v3, ...) </source>
Returns the average of the values specified. (More precisely, the value returned is the arithmetic mean.) Values that cannot be converted to numbers are ignored.
{{#invoke:math|round|value|precision}} {{#invoke:math|round|value=value|precision=precision}}
<source lang="lua"> mm._round(value, precision) </source>
Rounds a number to the specified precision.
{{#invoke:math|round|x|y}}
<source lang="lua"> mm._round(x, y) </source>
Gets x
modulo y
, or the remainder after x
has been divided by y
. This is accurate for integers up to 2^53; for larger integers Lua's modulo operator may return an erroneous value. This function deals with this problem by returning 0
if the modulo given by Lua's modulo operator is less than 0 or greater than y
.
{{#invoke:math|gcd|v1|v2|...}}
<source lang="lua"> mm._gcd(v1, v2, ...) </source>
Finds the greatest common divisor of the values specified. Values that cannot be converted to numbers are ignored.
{{#invoke:math|precision_format|value_string|precision}}
<source lang="lua"> mm._precision_format(value_string, precision) </source>
Rounds a number to the specified precision and formats according to rules originally used for {{Rnd}}. Output is a string.
<source lang="lua"> local number, number_string = mm._cleanNumber(number_string) </source>
A helper function that can be called from other Lua modules, but not from #invoke. This takes a string or a number value as input, and if the value can be converted to a number, cleanNumber returns the number and the number string. If the value cannot be converted to a number, cleanNumber returns nil, nil
.