ഘടകം:Math
This Lua module is used on 16,00,000+ pages, or roughly 303% of all pages. To avoid major disruption and server load, any changes should be tested in the module's /sandbox or /testcases subpages, or in your own module sandbox. The tested changes can be added to this page in a single edit. Consider discussing changes on the talk page before implementing them. |
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.
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.
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.
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.
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.
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
.
local z = {}
-- Generate random number
function z.random( frame )
first = tonumber(frame.args[1]) -- if it doesn't exist it's NaN, if not a number it's nil
second = tonumber(frame.args[2])
if first then -- if NaN or nil, will skip down to final return
if first <= second then -- could match if both nil, but already checked that first is a number in last line
return math.random(first, second)
end
return math.random(first)
end
return math.random()
end
-- Determine order of magnitude
function z.order(frame)
return z._order(tonumber(frame.args[1] or frame.args.x or 0))
end
function z._order(x)
if x == 0 then return 0 end
return math.floor(math.log10(math.abs(x)))
end
-- Determines precision of a number using the string representation
function z.precision( frame )
return z._precision( frame.args[1] or frame.args.x or '0' )
end
function z._precision( x )
x = string.upper( x )
-- Remove leading / trailing whitespace
x = x:match "^%s*(.-)%s*$";
local decimal = string.find( x, '.', 1, true )
local exponent_pos = string.find( x, 'E', 1, true )
local result = 0;
if exponent_pos ~= nil then
local exponent = string.sub( x, exponent_pos + 1 )
x = string.sub( x, 1, exponent_pos - 1 )
result = result - tonumber( exponent )
end
if decimal ~= nil then
result = result + string.len( x ) - decimal
return result
end
local pos = string.len( x );
while x:byte(pos) == string.byte('0') do
pos = pos - 1
result = result - 1
if pos <= 0 then
return 0
end
end
return result
end
-- Finds maximum argument
function z.max( frame )
if frame.args[1] == nil then
return ''
end
local max_value = tonumber( frame.args[1] )
local i = 2;
while frame.args[i] ~= nil do
local val = tonumber( frame.args[i] );
if val ~= nil then
if val > max_value then
max_value = val;
end
end
i = i + 1;
end
return max_value
end
-- Finds minimum argument
function z.min( frame )
if frame.args[1] == nil then
return ''
end
local min_value = tonumber( frame.args[1] )
local i = 2;
while frame.args[i] ~= nil do
local val = tonumber( frame.args[i] );
if val ~= nil then
if val < min_value then
min_value = val;
end
end
i = i + 1;
end
return min_value
end
-- Rounds a number to the specified precision and formats according to rules
-- originally used for {{template:Rnd}}
function z.round( frame )
-- For access to Mediawiki built-in formatter.
local lang = mw.getContentLanguage();
local value = tonumber( frame.args[1] or 0 );
local precision = tonumber( frame.args[2] or 0 );
local current_precision = z._precision( value );
-- If rounding off, truncate extra digits
if precision < current_precision then
local rescale = math.pow( 10, precision );
value = math.floor( value * rescale + 0.5 ) / rescale;
current_precision = z._precision( value );
end
local formatted_num = lang:formatNum( math.abs(value) );
local sign;
-- Use proper unary minus sign rather than ASCII default
if value < 0 then
sign = '−';
else
sign = '';
end
local order;
-- Handle cases requiring scientific notation
order = z._order( value );
if string.find( formatted_num, 'E', 1, true ) ~= nil or math.abs(order) >= 9 then
value = value * math.pow( 10, -order );
current_precision = current_precision + order;
precision = precision + order;
formatted_num = lang:formatNum( math.abs(value) );
else
order = 0;
end
formatted_num = sign .. formatted_num;
-- Pad with zeros, if needed
if current_precision < precision then
if current_precision <= 0 then
if precision > 0 then
local zero_sep = lang:formatNum( 1.1 );
formatted_num = formatted_num .. zero_sep:sub(2,2);
formatted_num = formatted_num .. string.rep( '0', precision );
end
else
formatted_num = formatted_num .. string.rep( '0', precision - current_precision );
end
end
-- Add exponential notation, if necessary.
if order ~= 0 then
-- Use proper unary minus sign rather than ASCII default
if order < 0 then
order = '−' .. lang:formatNum( math.abs(order) );
else
order = lang:formatNum( order );
end
formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>'
end
return formatted_num;
end
return z