Module:Math: Difference between revisions

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 = {}
require( "mw.language" );
 
-- Clean numeric value
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
 
-- Generate random number
Line 44 ⟶ 20:
end
 
--[[
-- Determine order of magnitude
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
 
--[[
-- Determines precision of a number using the string representation
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
 
--[[
-- 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 )
local args = frame.args;
Line 151 ⟶ 153:
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 )
local args = frame.args;
Line 175 ⟶ 189:
end
 
--[[
-- Rounds a number to specified precision
round
 
Rounds a number to specified precision
 
Usage:
{{#invoke:Math | round | value | precision }}
--]]
function z.round(frame)
local value, precision;
Line 193 ⟶ 215:
end
 
--[[
-- Rounds a number to the specified precision and formats according to rules
precision_format
-- originally used for {{template:Rnd}}. Output is a string.
 
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
 
Anonymous user