Jump to content

Module:Math: Difference between revisions

980 bytes added ,  11 years ago
Adds comments, slight reordering of functions.
m>Dragons flight
(Adds fraction trapping as an option)
m>Dragons flight
(Adds comments, slight reordering of functions.)
Line 1: Line 1:
--[[
This module provides a number of basic mathematical operations.
]]
local z = {}
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
-- Generate random number
Line 44: Line 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');
Line 61: Line 44:
end
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 )
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');
Line 127: Line 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;
Line 151: Line 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;
Line 175: Line 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;
Line 193: Line 215:
end
end


-- Rounds a number to the specified precision and formats according to rules  
--[[
-- originally used for {{template:Rnd}}.  Output is a string.
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.
Line 285: Line 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
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.