Open main menu

Changes

Module:Math

980 bytes added, 03:27, 5 March 2013
Adds comments, slight reordering of functions.
--[[
 
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
end
-- [[order Determine order of magnitudeof a number Usage: {{#invoke: Math | order | value }}]]
function z.order(frame)
local input_string = (frame.args[1] or frame.args.x or '0');
end
-- Determines [[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');
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 parentframe. Note, any values that do not evaluate to numbers are ignored.]]
function z.max( frame )
local args = frame.args;
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 parentframe. Note, any values that do not evaluate to numbers are ignored.]]
function z.min( frame )
local args = frame.args;
end
-- [[round Rounds a number to specified precision Usage: {{#invoke:Math | round | value | precision }} --]]
function z.round(frame)
local value, precision;
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.
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
return z
Anonymous user