Module:Math/doc

This module provides a number of mathematical functions. These functions can be used from #invoke or from other Lua modules.

Use 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:

&#x3c;syntaxhighlight lang="lua"&#x3e; local mm = require('Module:Math') &#x3c;/syntaxhighlight&#x3e;

(The &#x3c;code&#x3e;mm&#x3c;/code&#x3e; 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 &#x3c;code&#x3e;_&#x3c;/code&#x3e;, whereas #invoke functions are not.

random
&#x26;#123;{#invoke:math|random}} &#x26;#123;{#invoke:math|random|max_value}} &#x26;#123;{#invoke:math|random|min_value|max_value}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._random mm._random(max_value) mm._random(min_value, max_value) &#x3c;/syntaxhighlight&#x3e;

Generates a random number.


 * If no arguments are specified, the number produced is greater than or equal to 0 and less than 1.
 * If one argument is provided, the number produced is an integer between 1 and that argument. The argument must be a positive integer.
 * If two arguments are provided, the number produced is an integer between the first and second arguments. Both arguments must be integers, but can be negative.

This function will not work properly for numbers less than &#x26;minus;2&#x3c;sup&#x3e;32&#x3c;/sup&#x3e; and greater than 2&#x3c;sup&#x3e;32&#x3c;/sup&#x3e; &#x26;minus; 1. If you need to use numbers outside of this range, it is recommended that you use Module:Random.

order
&#x26;#123;{#invoke:math|order|n}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._order(n) &#x3c;/syntaxhighlight&#x3e;

Determines the order of magnitude of a number.

precision
&#x26;#123;{#invoke:math|precision|n}} &#x26;#123;{#invoke:math|precision|x=n}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._precision(number_string) &#x3c;/syntaxhighlight&#x3e;

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.

max
&#x26;#123;{#invoke:math|max|v1|v2|v3|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._max(v1, v2, v3, ...) &#x3c;/syntaxhighlight&#x3e;

Returns the maximum value from the values specified. Values that cannot be converted to numbers are ignored.

median
&#x26;#123;{#invoke:math|median|v1|v2|v3|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._median(v1, v2, v3, ...) &#x3c;/syntaxhighlight&#x3e;

Returns the median value from the values specified. Values that cannot be converted to numbers are ignored.

min
&#x26;#123;{#invoke:math|min|v1|v2|v3|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._min(v1, v2, v3, ...) &#x3c;/syntaxhighlight&#x3e;

Returns the minimum value from the values specified. Values that cannot be converted to numbers are ignored.

sum
&#x26;#123;{#invoke:math|sum|v1|v2|v3|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._sum(v1, v2, v3, ...) &#x3c;/syntaxhighlight&#x3e;

Returns the sum of the values specified. Values that cannot be converted to numbers are ignored.

average
&#x26;#123;{#invoke:math|average|v1|v2|v3|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._average(v1, v2, v3, ...) &#x3c;/syntaxhighlight&#x3e;

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.

round
&#x26;#123;{#invoke:math|round|value|precision}} &#x26;#123;{#invoke:math|round|value=value|precision=precision}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._round(value, precision) &#x3c;/syntaxhighlight&#x3e;

Rounds a number to the specified precision.

Note: As of October 2019, there is a bug in the display of some rounded numbers. When trying to round a number that rounds to "n.0", like "1.02", to the nearest tenth of a digit (i.e. 1), this function should display "1.0", but it unexpectedly displays "1". Use the precision_format parameter instead.

log10
&#x26;#123;{#invoke:math | log10 | x}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._log10(x) &#x3c;/syntaxhighlight&#x3e;

Returns &#x3c;code&#x3e;log&#x3c;sub&#x3e;10&#x3c;/sub&#x3e;(x)&#x3c;/code&#x3e;, the logarithm of x using base 10.

mod
&#x26;#123;{#invoke:math|mod|x|y}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._mod(x, y) &#x3c;/syntaxhighlight&#x3e;

Gets &#x3c;code&#x3e;x&#x3c;/code&#x3e; modulo &#x3c;code&#x3e;y&#x3c;/code&#x3e;, or the remainder after &#x3c;code&#x3e;x&#x3c;/code&#x3e; has been divided by &#x3c;code&#x3e;y&#x3c;/code&#x3e;. This is accurate for integers up to 2&#x3c;sup&#x3e;53&#x3c;/sup&#x3e;; for larger integers Lua's modulo operator may return an erroneous value. This function deals with this problem by returning &#x3c;code&#x3e;0&#x3c;/code&#x3e; if the modulo given by Lua's modulo operator is less than 0 or greater than &#x3c;code&#x3e;y&#x3c;/code&#x3e;.

gcd
&#x26;#123;{#invoke:math|gcd|v1|v2|...}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._gcd(v1, v2, ...) &#x3c;/syntaxhighlight&#x3e;

Finds the greatest common divisor of the values specified. Values that cannot be converted to numbers are ignored.

precision_format
&#x26;#123;{#invoke:math|precision_format|value_string|precision}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._precision_format(value_string, precision) &#x3c;/syntaxhighlight&#x3e;

Rounds a number to the specified precision and formats according to rules originally used for Rnd. Output is a string.

Parameter &#x3c;var&#x3e;precision&#x3c;/var&#x3e; should be an integer number of digits after the decimal point. Negative values are permitted. Non-integers give unexpected results. Positive values greater than the input precision add zero-padding, negative values greater than the input order can consume all digits.

Formatting 8,765.567 with gives:

divide
&#x26;#123;{#invoke:Math|divide|x|y|round=|precision=}}

&#x3c;syntaxhighlight lang="lua"&#x3e; mm._divide(x, y, round, precision) &#x3c;/syntaxhighlight&#x3e;

Divide x by y.


 * If y if not a number, it is returned.
 * Otherwise, if x is not a number, it is returned.
 * If round is true ("yes" for #invoke), the result has no decimals
 * Precision indicates how many digits of precision the result should have

If any of the arguments contain HTML tags, they are returned unchanged, allowing any errors in calculating the arguments to the division function to be propagated to the calling template.

cleanNumber
&#x3c;syntaxhighlight lang="lua"&#x3e; local number, number_string = mm._cleanNumber(number_string) &#x3c;/syntaxhighlight&#x3e;

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 &#x3c;code&#x3e;nil, nil&#x3c;/code&#x3e;.