Difference between revisions of "Module:Math/doc"

From blackwiki
Jump to navigation Jump to search
blackwiki>DePiep
(hatnote)
blackwiki>WOSlinker
m (change source to syntaxhighlight)
Line 7: Line 7:
 
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:
 
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">
+
<syntaxhighlight lang="lua">
 
local mm = require('Module:Math')
 
local mm = require('Module:Math')
</source>
+
</syntaxhighlight>
  
 
(The <code>mm</code> variable stands for '''M'''odule '''M'''ath; you can choose something more descriptive if you prefer.)
 
(The <code>mm</code> variable stands for '''M'''odule '''M'''ath; you can choose something more descriptive if you prefer.)
Line 22: Line 22:
 
  &#123;{#invoke:math|random|''min_value''|''max_value''}}
 
  &#123;{#invoke:math|random|''min_value''|''max_value''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._random()
 
mm._random()
 
mm._random(max_value)
 
mm._random(max_value)
 
mm._random(min_value, max_value)
 
mm._random(min_value, max_value)
</source>
+
</syntaxhighlight>
  
 
Generates a random number.
 
Generates a random number.
Line 40: Line 40:
 
  &#123;{#invoke:math|order|''n''}}
 
  &#123;{#invoke:math|order|''n''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._order(n)
 
mm._order(n)
</source>
+
</syntaxhighlight>
  
 
Determines the [[order of magnitude]] of a number.
 
Determines the [[order of magnitude]] of a number.
Line 51: Line 51:
 
  &#123;{#invoke:math|precision|x=''n''}}
 
  &#123;{#invoke:math|precision|x=''n''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._precision(number_string)
 
mm._precision(number_string)
</source>
+
</syntaxhighlight>
  
 
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".
 
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".
Line 63: Line 63:
 
  &#123;{#invoke:math|max|''v1''|''v2''|''v3''|...}}
 
  &#123;{#invoke:math|max|''v1''|''v2''|''v3''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._max(v1, v2, v3, ...)
 
mm._max(v1, v2, v3, ...)
</source>
+
</syntaxhighlight>
  
 
Returns the maximum value from the values specified. Values that cannot be converted to numbers are ignored.
 
Returns the maximum value from the values specified. Values that cannot be converted to numbers are ignored.
Line 73: Line 73:
 
  &#123;{#invoke:math|median|''v1''|''v2''|''v3''|...}}
 
  &#123;{#invoke:math|median|''v1''|''v2''|''v3''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._median(v1, v2, v3, ...)
 
mm._median(v1, v2, v3, ...)
</source>
+
</syntaxhighlight>
  
 
Returns the [[median]]  value from the values specified. Values that cannot be converted to numbers are ignored.
 
Returns the [[median]]  value from the values specified. Values that cannot be converted to numbers are ignored.
Line 83: Line 83:
 
  &#123;{#invoke:math|min|''v1''|''v2''|''v3''|...}}
 
  &#123;{#invoke:math|min|''v1''|''v2''|''v3''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._min(v1, v2, v3, ...)
 
mm._min(v1, v2, v3, ...)
</source>
+
</syntaxhighlight>
  
 
Returns the minimum value from the values specified. Values that cannot be converted to numbers are ignored.
 
Returns the minimum value from the values specified. Values that cannot be converted to numbers are ignored.
Line 93: Line 93:
 
  &#123;{#invoke:math|sum|''v1''|''v2''|''v3''|...}}
 
  &#123;{#invoke:math|sum|''v1''|''v2''|''v3''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._sum(v1, v2, v3, ...)
 
mm._sum(v1, v2, v3, ...)
</source>
+
</syntaxhighlight>
  
 
Returns the sum of the values specified. Values that cannot be converted to numbers are ignored.
 
Returns the sum of the values specified. Values that cannot be converted to numbers are ignored.
Line 103: Line 103:
 
  &#123;{#invoke:math|average|''v1''|''v2''|''v3''|...}}
 
  &#123;{#invoke:math|average|''v1''|''v2''|''v3''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._average(v1, v2, v3, ...)
 
mm._average(v1, v2, v3, ...)
</source>
+
</syntaxhighlight>
  
 
Returns the average of the values specified. (More precisely, the value returned is the [[Mean#Arithmetic mean (AM)|arithmetic mean]].) Values that cannot be converted to numbers are ignored.
 
Returns the average of the values specified. (More precisely, the value returned is the [[Mean#Arithmetic mean (AM)|arithmetic mean]].) Values that cannot be converted to numbers are ignored.
Line 114: Line 114:
 
  &#123;{#invoke:math|round|value=''value''|precision=''precision''}}
 
  &#123;{#invoke:math|round|value=''value''|precision=''precision''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._round(value, precision)
 
mm._round(value, precision)
</source>
+
</syntaxhighlight>
  
 
[[Rounding|Rounds]] a number to the specified precision.  
 
[[Rounding|Rounds]] a number to the specified precision.  
Line 126: Line 126:
 
  &#123;{#invoke:math | log10 | ''x''}}
 
  &#123;{#invoke:math | log10 | ''x''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._log10(x)
 
mm._log10(x)
</source>
+
</syntaxhighlight>
  
 
Returns <code>log<sub>10</sub>(''x'')</code>, the [[logarithm]] of ''x'' using base 10.
 
Returns <code>log<sub>10</sub>(''x'')</code>, the [[logarithm]] of ''x'' using base 10.
Line 136: Line 136:
 
  &#123;{#invoke:math|mod|''x''|''y''}}
 
  &#123;{#invoke:math|mod|''x''|''y''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._mod(x, y)
 
mm._mod(x, y)
</source>
+
</syntaxhighlight>
  
 
Gets <code>''x''</code> [[Modulo operation|modulo]] <code>''y''</code>, or the remainder after <code>''x''</code> has been divided by <code>''y''</code>. This is accurate for integers up to 2<sup>53</sup>; for larger integers Lua's modulo operator may return an erroneous value. This function deals with this problem by returning <code>0</code> if the modulo given by Lua's modulo operator is less than 0 or greater than <code>''y''</code>.
 
Gets <code>''x''</code> [[Modulo operation|modulo]] <code>''y''</code>, or the remainder after <code>''x''</code> has been divided by <code>''y''</code>. This is accurate for integers up to 2<sup>53</sup>; for larger integers Lua's modulo operator may return an erroneous value. This function deals with this problem by returning <code>0</code> if the modulo given by Lua's modulo operator is less than 0 or greater than <code>''y''</code>.
Line 146: Line 146:
 
  &#123;{#invoke:math|gcd|''v1''|''v2''|...}}
 
  &#123;{#invoke:math|gcd|''v1''|''v2''|...}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._gcd(v1, v2, ...)
 
mm._gcd(v1, v2, ...)
</source>
+
</syntaxhighlight>
  
 
Finds the [[greatest common divisor]] of the values specified. Values that cannot be converted to numbers are ignored.
 
Finds the [[greatest common divisor]] of the values specified. Values that cannot be converted to numbers are ignored.
Line 156: Line 156:
 
  &#123;{#invoke:math|precision_format|''value_string''|''precision''}}
 
  &#123;{#invoke:math|precision_format|''value_string''|''precision''}}
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
mm._precision_format(value_string, precision)
 
mm._precision_format(value_string, precision)
</source>
+
</syntaxhighlight>
  
 
Rounds a number to the specified precision and formats according to rules originally used for {{tl|Rnd}}.  Output is a string.
 
Rounds a number to the specified precision and formats according to rules originally used for {{tl|Rnd}}.  Output is a string.
Line 170: Line 170:
 
== cleanNumber ==
 
== cleanNumber ==
  
<source lang="lua">
+
<syntaxhighlight lang="lua">
 
local number, number_string = mm._cleanNumber(number_string)
 
local number, number_string = mm._cleanNumber(number_string)
</source>
+
</syntaxhighlight>
  
 
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 <code>nil, nil</code>.
 
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 <code>nil, nil</code>.

Revision as of 13:19, 7 July 2020

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

{{#invoke:math|random}}
{{#invoke:math|random|max_value}}
{{#invoke:math|random|min_value|max_value}}
mm._random()
mm._random(max_value)
mm._random(min_value, max_value)

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 −232 and greater than 232 − 1. If you need to use numbers outside of this range, it is recommended that you use Module:Random.

order

{{#invoke:math|order|n}}
mm._order(n)

Determines the order of magnitude of a number.

precision

{{#invoke:math|precision|n}}
{{#invoke:math|precision|x=n}}
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

{{#invoke:math|max|v1|v2|v3|...}}
mm._max(v1, v2, v3, ...)

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

median

{{#invoke:math|median|v1|v2|v3|...}}
mm._median(v1, v2, v3, ...)

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

min

{{#invoke:math|min|v1|v2|v3|...}}
mm._min(v1, v2, v3, ...)

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

sum

{{#invoke:math|sum|v1|v2|v3|...}}
mm._sum(v1, v2, v3, ...)

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

average

{{#invoke:math|average|v1|v2|v3|...}}
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

{{#invoke:math|round|value|precision}}
{{#invoke:math|round|value=value|precision=precision}}
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. |r=1), this function should display "1.0", but it unexpectedly displays "1". Use the |precision_format= parameter instead.

log10

{{#invoke:math | log10 | x}}
mm._log10(x)

Returns log10(x), the logarithm of x using base 10.

mod

{{#invoke:math|mod|x|y}}
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

{{#invoke:math|gcd|v1|v2|...}}
mm._gcd(v1, v2, ...)

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

precision_format

{{#invoke:math|precision_format|value_string|precision}}
mm._precision_format(value_string, precision)

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

Parameter precision should be an integer. Non-integers give unexpected result

  • 1.234567
  • 2: 1.23
  • 2.3: 1.2329205947231
  • 2.7: 1.2350673729657

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.

See also