Modul:Math: Unterschied zwischen den Versionen
Adds comments, slight reordering of functions.
(Adds fraction trapping as an option) |
(Adds comments, slight reordering of functions.) |
||
Zeile 1: | Zeile 1: | ||
--[[ | |||
This module provides a number of basic mathematical operations. | |||
]] | |||
local z = {} | local z = {} | ||
-- Generate random number | -- Generate random number | ||
Zeile 44: | Zeile 20: | ||
end | end | ||
-- Determine order of magnitude | --[[ | ||
order | |||
Determine order of magnitude of a number | |||
Usage: | |||
{{#invoke: Math | order | value }} | |||
]] | |||
function z.order(frame) | function z.order(frame) | ||
local input_string = (frame.args[1] or frame.args.x or '0'); | local input_string = (frame.args[1] or frame.args.x or '0'); | ||
Zeile 61: | Zeile 44: | ||
end | end | ||
-- | --[[ | ||
precision | |||
Detemines the precision of a number using the string representation | |||
Usage: | |||
{{ #invoke: Math | precision | value }} | |||
]] | |||
function z.precision( frame ) | function z.precision( frame ) | ||
local input_string = (frame.args[1] or frame.args.x or '0'); | local input_string = (frame.args[1] or frame.args.x or '0'); | ||
Zeile 127: | Zeile 117: | ||
end | end | ||
-- Finds maximum argument | --[[ | ||
max | |||
Finds the maximum argument | |||
Usage: | |||
{{#invoke:Math| max | value1 | value2 | ... }} | |||
OR | |||
{{#invoke:Math| max }} | |||
When used with no arguments, it takes its input from the parent | |||
frame. Note, any values that do not evaluate to numbers are ignored. | |||
]] | |||
function z.max( frame ) | function z.max( frame ) | ||
local args = frame.args; | local args = frame.args; | ||
Zeile 151: | Zeile 153: | ||
end | end | ||
-- Finds minimum argument | --[[ | ||
min | |||
Finds the minimum argument | |||
Usage: | |||
{{#invoke:Math| min | value1 | value2 | ... }} | |||
OR | |||
{{#invoke:Math| min }} | |||
When used with no arguments, it takes its input from the parent | |||
frame. Note, any values that do not evaluate to numbers are ignored. | |||
]] | |||
function z.min( frame ) | function z.min( frame ) | ||
local args = frame.args; | local args = frame.args; | ||
Zeile 175: | Zeile 189: | ||
end | end | ||
-- Rounds a number to specified precision | --[[ | ||
round | |||
Rounds a number to specified precision | |||
Usage: | |||
{{#invoke:Math | round | value | precision }} | |||
--]] | |||
function z.round(frame) | function z.round(frame) | ||
local value, precision; | local value, precision; | ||
Zeile 193: | Zeile 215: | ||
end | end | ||
-- Rounds a number to the specified precision and formats according to rules | --[[ | ||
precision_format | |||
Rounds a number to the specified precision and formats according to rules | |||
originally used for {{template:Rnd}}. Output is a string. | |||
Usage: | |||
{{#invoke: Math | precision_format | number | precision }} | |||
]] | |||
function z.precision_format( frame ) | function z.precision_format( frame ) | ||
-- For access to Mediawiki built-in formatter. | -- For access to Mediawiki built-in formatter. | ||
Zeile 285: | Zeile 314: | ||
return formatted_num; | return formatted_num; | ||
end | |||
--[[ | |||
Helper function that interprets the input numerically. If the | |||
input does not appear to be a number, attempts evaluating it as | |||
a parser functions expression. | |||
]] | |||
function z._cleanNumber( frame, number_string ) | |||
if number_string == nil or number_string:len() == 0 then | |||
return nil, nil; | |||
end | |||
-- Attempt basic conversion | |||
local number = tonumber( number_string ) | |||
-- If failed, attempt to evaluate input as an expression | |||
if number == nil then | |||
local attempt = frame:preprocess( '{{#expr: ' .. number_string .. '}}' ); | |||
attempt = tonumber( attempt ); | |||
if attempt ~= nil then | |||
number = attempt; | |||
number_string = tostring( number ); | |||
else | |||
number = nil; | |||
number_string = nil; | |||
end | |||
else | |||
-- String is valid but may contain padding, clean it. | |||
number_string = number_string:match( "^%s*(.-)%s*$" ); | |||
end | |||
return number, number_string; | |||
end | end | ||
return z | return z |