Anonymous user
Adds comments, slight reordering of functions.
en>Dragons flight (Adds fraction trapping as an option) |
en>Dragons flight (Adds comments, slight reordering of functions.) |
||
Line 1:
--[[
This module provides a number of basic mathematical operations.
]]
local z = {}
-- Generate random number
Line 44 ⟶ 20:
end
--[[
order
Determine order of magnitude of a number
Usage:
{{#invoke: Math | order | value }}
]]
function z.order(frame)
local input_string = (frame.args[1] or frame.args.x or '0');
Line 61 ⟶ 44:
end
--[[
precision
Detemines the precision of a number using the string representation
Usage:
{{ #invoke: Math | precision | value }}
]]
function z.precision( frame )
local input_string = (frame.args[1] or frame.args.x or '0');
Line 127 ⟶ 117:
end
--[[
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 )
local args = frame.args;
Line 151 ⟶ 153:
end
--[[
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 )
local args = frame.args;
Line 175 ⟶ 189:
end
--[[
round
Rounds a number to specified precision
Usage:
{{#invoke:Math | round | value | precision }}
--]]
function z.round(frame)
local value, precision;
Line 193 ⟶ 215:
end
--[[
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 )
-- For access to Mediawiki built-in formatter.
Line 285 ⟶ 314:
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
|