모듈:Math: 두 판 사이의 차이

Revi HQ
내용 추가됨 내용 삭제됨
편집 요약 없음
(add max and min)
1번째 줄: 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
52번째 줄: 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

2013년 2월 22일 (금) 02:06 판

이 모듈에 대한 설명문서는 모듈:Math/설명문서에서 만들 수 있습니다

local z = {}

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

    if first then -- if NaN or nil, will skip down to final return
        if first <= second then -- could match if both nil, but already checked that first is a number in last line
            return math.random(first, second)
        end
        return math.random(first)
    end   
    return math.random()
end

function z.precision( frame )
    return z._precision( frame.args[1] or frame.args.x or '0' )
end

-- Determines precision of a number using the string representation
function z._precision( x )
    x = string.upper( x )

    -- Remove leading / trailing whitespace
    x = x:match "^%s*(.-)%s*$";

    local decimal = string.find( x, '.', 1, true )
    local exponent_pos = string.find( x, 'E', 1, true )
    local result = 0;
    
    if exponent_pos ~= nil then
        local exponent = string.sub( x, exponent_pos + 1 )
        x = string.sub( x, 1, exponent_pos - 1 )
        result = result - tonumber( exponent )
    end    
    
    if decimal ~= nil then
        result = result + string.len( x ) - decimal
        return result
    end
        
    local pos = string.len( x );
    while x:byte(pos) == string.byte('0') do
        pos = pos - 1
        result = result - 1
        if pos <= 0 then
            return 0
        end
    end
    
    return result
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