Module:Math: Difference between revisions

921 bytes added ,  11 years ago
add max and min
m>Dragons flight
No edit summary
m>Dragons flight
(add max and min)
Line 1: Line 1:
local z = {}
local z = {}


-- Generate random number
function z.random( frame )
function z.random( frame )
     first = tonumber(frame.args[1]) -- if it doesn't exist it's NaN, if not a number it's nil
     first = tonumber(frame.args[1]) -- if it doesn't exist it's NaN, if not a number it's nil
Line 52: Line 53:
end
end


-- Finds maximum argument
function z.max( frame )
    if frame.args[1] == nil then
        return ''
    end
    local max_value = tonumber( frame.args[1] )
   
    local i = 2;
    while frame.args[i] ~= nil do
        local val = tonumber( frame.args[i] );
        if val ~= nil then
            if val > max_value then
                max_value = val;
            end
        end       
        i = i + 1;
    end
 
    return max_value
end
-- Finds minimum argument
function z.min( frame )
    if frame.args[1] == nil then
        return ''
    end
    local min_value = tonumber( frame.args[1] )
   
    local i = 2;
    while frame.args[i] ~= nil do
        local val = tonumber( frame.args[i] );
        if val ~= nil then
            if val < min_value then
                min_value = val;
            end
        end       
        i = i + 1;
    end
 
    return min_value
end
return z
return z
Anonymous user