CargoAdmin, Bureaucrats, Moderators (CommentStreams), fileuploaders, Interface administrators, newuser, Push subscription managers, Suppressors, Administrators
14,662
edits
 >Tomoneill  (copy from https://en.wikipedia.org/w/index.php?title=Module:Math/doc&oldid=861908428)  | 
				m (1 revision imported)  | 
				||
| (One intermediate revision by the same user not shown) | |||
| Line 1: | Line 1: | ||
{{for|''formatting'' mathematical expressions, LATEX-style|Template:Math}}  | |||
{{High-risk|1,607,000+}}  | {{High-risk|1,607,000+}}  | ||
{{module rating|protected}}  | {{module rating|protected}}  | ||
This module provides a number of mathematical functions. These functions can be used from #invoke or from other Lua modules.  | This module provides a number of mathematical functions. These functions can be used from #invoke or from other Lua modules.  | ||
| Line 8: | 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:  | ||
<  | <syntaxhighlight lang="lua">  | ||
local mm = require('Module:Math')  | local mm = require('Module:Math')  | ||
</  | </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 23: | Line 22: | ||
  {{#invoke:math|random|''min_value''|''max_value''}}  |   {{#invoke:math|random|''min_value''|''max_value''}}  | ||
<  | <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)  | ||
</  | </syntaxhighlight>  | ||
Generates a random number.  | Generates a random number.  | ||
| Line 41: | Line 40: | ||
  {{#invoke:math|order|''n''}}  |   {{#invoke:math|order|''n''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._order(n)  | mm._order(n)  | ||
</  | </syntaxhighlight>  | ||
Determines the [[order of magnitude]] of a number.  | Determines the [[order of magnitude]] of a number.  | ||
| Line 52: | Line 51: | ||
  {{#invoke:math|precision|x=''n''}}  |   {{#invoke:math|precision|x=''n''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._precision(number_string)  | mm._precision(number_string)  | ||
</  | </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 64: | Line 63: | ||
  {{#invoke:math|max|''v1''|''v2''|''v3''|...}}  |   {{#invoke:math|max|''v1''|''v2''|''v3''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._max(v1, v2, v3, ...)  | mm._max(v1, v2, v3, ...)  | ||
</  | </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 72: | Line 71: | ||
== median  ==  | == median  ==  | ||
  {{#invoke:median  |   {{#invoke:math|median|''v1''|''v2''|''v3''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._median(v1, v2, v3, ...)  | mm._median(v1, v2, v3, ...)  | ||
</  | </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 84: | Line 83: | ||
  {{#invoke:math|min|''v1''|''v2''|''v3''|...}}  |   {{#invoke:math|min|''v1''|''v2''|''v3''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._min(v1, v2, v3, ...)  | mm._min(v1, v2, v3, ...)  | ||
</  | </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 94: | Line 93: | ||
  {{#invoke:math|sum|''v1''|''v2''|''v3''|...}}  |   {{#invoke:math|sum|''v1''|''v2''|''v3''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._sum(v1, v2, v3, ...)  | mm._sum(v1, v2, v3, ...)  | ||
</  | </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 104: | Line 103: | ||
  {{#invoke:math|average|''v1''|''v2''|''v3''|...}}  |   {{#invoke:math|average|''v1''|''v2''|''v3''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._average(v1, v2, v3, ...)  | mm._average(v1, v2, v3, ...)  | ||
</  | </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 115: | Line 114: | ||
  {{#invoke:math|round|value=''value''|precision=''precision''}}  |   {{#invoke:math|round|value=''value''|precision=''precision''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._round(value, precision)  | mm._round(value, precision)  | ||
</  | </syntaxhighlight>  | ||
[[Rounding|Rounds]] a number to the specified precision.  | [[Rounding|Rounds]] a number to the specified precision{{clarify|reason=What method exactly is used?}}.  | ||
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. {{para|r|1}}), this function should display "1.0", but it unexpectedly displays "1". Use the {{para|precision_format}} parameter instead.  | |||
== log10 ==  | == log10 ==  | ||
| Line 125: | Line 126: | ||
  {{#invoke:math | log10 | ''x''}}  |   {{#invoke:math | log10 | ''x''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._log10(x)  | mm._log10(x)  | ||
</  | </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 135: | Line 136: | ||
  {{#invoke:math|mod|''x''|''y''}}  |   {{#invoke:math|mod|''x''|''y''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._mod(x, y)  | mm._mod(x, y)  | ||
</  | </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 145: | Line 146: | ||
  {{#invoke:math|gcd|''v1''|''v2''|...}}  |   {{#invoke:math|gcd|''v1''|''v2''|...}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._gcd(v1, v2, ...)  | mm._gcd(v1, v2, ...)  | ||
</  | </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 155: | Line 156: | ||
  {{#invoke:math|precision_format|''value_string''|''precision''}}  |   {{#invoke:math|precision_format|''value_string''|''precision''}}  | ||
<  | <syntaxhighlight lang="lua">  | ||
mm._precision_format(value_string, precision)  | mm._precision_format(value_string, precision)  | ||
</  | </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.  | ||
Parameter <var>precision</var> 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 {{tlc|#invoke:Math|precision_format|8765.567|<var>precision</var>}} gives:  | |||
{|class="wikitable"  | |||
|+  | |||
!<code><var>precision</var></code>!!Result  | |||
|-  | |||
|style="text-align:right"|2||{{#invoke:Math|precision_format|8765.567|2}}  | |||
|-  | |||
|style="text-align:right"| -2||{{#invoke:Math|precision_format|8765.567|-2}}  | |||
|-  | |||
|style="text-align:right"|6||{{#invoke:Math|precision_format|8765.567|6}}  | |||
|-  | |||
|style="text-align:right"| -6||{{#invoke:Math|precision_format|8765.567|-6}}  | |||
|-  | |||
|style="text-align:right"|2.5||{{#invoke:Math|precision_format|8765.567|2.5}}  | |||
|-  | |||
|style="text-align:right"| -2.5||{{#invoke:Math|precision_format|8765.567|-2.5}}  | |||
|}  | |||
== divide ==  | |||
 {{#invoke:Math|divide|''x''|''y''|''round=''|''precision=''}}  | |||
<syntaxhighlight lang="lua">  | |||
mm._divide(x, y, round, precision)  | |||
</syntaxhighlight>  | |||
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 ==  | == cleanNumber ==  | ||
<  | <syntaxhighlight lang="lua">  | ||
local number, number_string = mm._cleanNumber(number_string)  | local number, number_string = mm._cleanNumber(number_string)  | ||
</  | </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>.  | ||
==See also==  | |||
{{Math templates}}  | |||
edits