Module:Math/doc explained

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:

local mm = require('Module:Math')

(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.

random

See also: Module:Random.

{} {} {}

mm._randommm._random(max_value)mm._random(min_value, max_value)

Generates a random number.

This function will not work properly for numbers less than -232 and greater than 232 - 1. If you need to use numbers outside of this range, it is recommended that you use .

order

{}

mm._order(n)

Determines the order of magnitude of a number.

precision

{} {}

mm._precision(number_string)

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

{}

mm._max(v1, v2, v3, ...)

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

median

{}

mm._median(v1, v2, v3, ...)

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

min

{}

mm._min(v1, v2, v3, ...)

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

sum

{}

mm._sum(v1, v2, v3, ...)

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

average

{}

mm._average(v1, v2, v3, ...)

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

{} {}

mm._round(value, precision)

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.), this function should display "1.0", but it unexpectedly displays "1". Use the parameter instead.

log10

{}

mm._log10(x)

Returns log<sub>10</sub>(''x''), the logarithm of x using base 10.

mod

{}

mm._mod(x, y)

Gets ''x'' modulo ''y'', or the remainder after ''x'' has been divided by ''y''. This is accurate for integers up to 253; 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''.

gcd

{}

mm._gcd(v1, v2, ...)

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

precision_format

{}

mm._precision_format(value_string, precision)

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

Parameter precision 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:

!<var>precision</var>!!Result
2
-2
6
-6
2.5
-2.5

divide

{}

mm._divide(x, y, round, precision)

Divide x by y.

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

local number, number_string = mm._cleanNumber(number_string)

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.