Mòdul:Math: diferència entre les revisions

De la Viquipèdia, l'enciclopèdia lliure
Contingut suprimit Contingut afegit
actualització cleanNumber
Es desfà la revisió 14257465 de Vriullop (Discussió), cal reorganitzar cleanNumber
Línia 367: Línia 367:
a parser functions expression.
a parser functions expression.
]]
]]

function z._cleanNumber(number_string)
function z._cleanNumber( frame, number_string )
if type(number_string) == 'number' then
if number_string == nil or number_string:len() == 0 then
return nil, nil;
-- We were passed a number, so we don't need to do any processing.
end
return number_string, tostring(number_string)
elseif type(number_string) ~= 'string' or not number_string:find('%S') then
-- Attempt basic conversion
-- We were passed a non-string or a blank string, so exit.
local number = tonumber( number_string )
return nil, nil;
end
-- If failed, attempt to evaluate input as an expression
if number == nil then
-- Attempt basic conversion
local number = tonumber(number_string)
local attempt = frame:callParserFunction('#expr', number_string)
attempt = tonumber( attempt );
if attempt ~= nil then
-- If failed, attempt to evaluate input as an expression
if number == nil then
number = attempt;
number_string = tostring( number );
local success, result = pcall(mw.ext.ParserFunctions.expr, number_string)
else
if success then
number = tonumber(result)
number = nil;
number_string = tostring(number)
number_string = nil;
end
else
else
number = nil
-- String is valid but may contain padding, clean it.
number_string = nil
number_string = number_string:match( "^%s*(.-)%s*$" );
end
end
else
number_string = number_string:match("^%s*(.-)%s*$") -- String is valid but may contain padding, clean it.
return number, number_string;
number_string = number_string:match("^%+(.*)$") or number_string -- Trim any leading + signs.
if number_string:find('^%-?0[xX]') then
-- Number is using 0xnnn notation to indicate base 16; use the number that Lua detected instead.
number_string = tostring(number)
end
end
return number, number_string
end
end



Revisió del 23:39, 5 nov 2014

Icona de documentació de mòdul Documentació del mòdul [ mostra ] [ modifica el codi ] [ mostra l'historial ] [ refresca ]

Mòdul Math (codi · ús · discussió · proves · tests · casos prova | subpàgines · enllaços)

A continuació es mostra la documentació transclosa de la subpàgina /ús. [salta a la caixa de codi]


Aquest mòdul proporciona operacions matemàtiques bàsiques.

De moment, s'utilitza només la notació amb punt decimal. Els paràmetres d'entrada s'haurien de convertir amb {{formatnum:<num>|R}} (vegeu {{puntdecimal}}) i els valors retornats es poden formatejar de nou amb {{formatnum:<num>}}.

Funcions:

random

Retorna un número pseudoaleatori. Sintaxi:

{{#invoke:math|random}}

Sense cap argument, genera un número real en el rang [0,1) (amb l'1 exclòs), per exemple 0.27327761751287

{{#invoke:math|random|<número>}}

Proporcionant un número enter, genera un número enter en el rang [1,número]. Si és negatiu, el rang serà [número,-1].

{{#invoke:math|random|<primer>|<últim>}}

Proporcionant dos números enters, genera un número enter en el rang [primer,últim].

max

Troba el valor màxim dels arguments. Sintaxi:

{{#invoke:math|max|<valor 1>|<valor 2>|...}}
{{#invoke:math|max}}

Quan s'usa sense arguments, pren l'entrada del marc superior. Qualsevol valor numèric no vàlid és ignorat.

min

Troba el valor mínim dels arguments. Sintaxi:

{{#invoke:math|min|<valor 1>|<valor 2>|...}}
{{#invoke:math|min}}

Quan s'usa sense arguments, pren l'entrada del marc superior. Qualsevol valor numèric no vàlid és ignorat.

average

Troba la mitjana dels valors proporcionats. Sintaxi:

{{#invoke:math|average|<valor 1>|<valor 2>|...}}
{{#invoke:math|average}}

Quan s'usa sense arguments, pren l'entrada del marc superior. Qualsevol valor numèric no vàlid és ignorat.

order

Determina l'ordre de magnitud d'un número. Sintaxi:

{{#invoke:math|order|<número>}}
{{#invoke:math|order|x=<número>}}

Per exemple: 100 → 2, 0.001 → -3

precision

Determina la precisió de un número. Sintaxi:

{{#invoke:math|precision|<número>}}
{{#invoke:math|precision|x=<número>}}

És l'invers de l'ordre de magnitud: indica el número de xifres decimals, inclosos zeros a la dreta, i una precisió negativa indica la potència de 10 de la primera xifra significativa.

round

Arrodoneix un número amb una precisió determinada. Sintaxi:

{{#invoke:math|round|<valor>|<precisió>}}
{{#invoke:math|round|value=<valor>|precision=<precisió>}}

La precisió indica el número de xifres decimals. Una precisió negativa indica el múltiple de la potència de 10.

precision_format

Arrodoneix un número amb una precisió determinada i retorna el valor en el format numèric local o en notació científica quan fa falta. Sintaxi:

{{#invoke:math|precision_format|<número>|<precisió>}}

El número es pot expressar amb la notació per exemple 4E9 i quan el valor retornat té un ordre de magnitud de 9 o superior s'expressa per exemple 4×109

_cleanNumber

Funció auxiliar que avalua si una entrada és numèrica o si la pot convertir. Retorna el valor numèric i el valor cadena. Pot ser útil en altres mòduls.

Vegeu també

--[[

This module provides a number of basic mathematical operations.

]]
local z = {}

-- Generate random number
function z.random(frame)
    local first = tonumber(frame.args[1])
    local second = tonumber(frame.args[2])
    local seed = tonumber(frame.args.seed)
    if seed then
        math.randomseed(seed * os.time())    -- inicialització pseudoaleatòria
    end
    if first and second then
        if first < second then
            return math.random(first,second)    -- enter entre [first,second]
        else
            return math.random(second,first)
        end
    elseif first then
        if first > 0 then
            return math.random(first)    -- enter entre [1,first]
        else
            return math.random(first,-1)
        end
    else
        return math.random()    -- número real entre [0,1)
    end
end

--[[
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');
    local input_number;
    
    input_number = z._cleanNumber( frame, input_string );
    if input_number == nil then
        return '<strong class="error">Error de format: l\'ordre de magnitud ha de ser numèric</strong>'
    else
        return z._order( input_number )
    end    
end
function z._order(x)
    if x == 0 then return 0 end
    return math.floor(math.log10(math.abs(x)))
end

--[[
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');
    local trap_fraction = frame.args.check_fraction or false;
    local input_number;
    
    if type( trap_fraction ) == 'string' then
        trap_fraction = trap_fraction:lower();
        if trap_fraction == 'false' or trap_fraction == '0' or
                trap_fraction == 'no' or trap_fraction == '' then
            trap_fraction = false;
        else
            trap_fraction = true;
        end
    end
    
    if trap_fraction then
        local pos = string.find( input_string, '/', 1, true );
        if pos ~= nil then
            if string.find( input_string, '/', pos + 1, true ) == nil then
                local denominator = string.sub( input_string, pos+1, -1 );
                local denom_value = tonumber( denominator );
                if denom_value ~= nil then
                    return math.log10(denom_value);
                end
            end                        
        end
    end    
    
    input_number, input_string = z._cleanNumber( frame, input_string );
    if input_string == nil then
        return '<strong class="error">Error de format: el valor de precisió ha de ser numèric</strong>'
    else
        return z._precision( input_string )
    end    
end
function z._precision( x )    
    x = string.upper( x )

    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

--[[
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;
    
    if args[1] == nil then
        local parent = frame:getParent();
        args = parent.args;
    end
    local max_value = nil;
    
    local i = 1;
    while args[i] ~= nil do
        local val = z._cleanNumber( frame, args[i] );
        if val ~= nil then
            if max_value == nil or val > max_value then
                max_value = val;
            end
        end        
        i = i + 1;
    end
  
    return max_value
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 parent
frame.  Note, any values that do not evaluate to numbers are ignored.
]]
function z.min( frame )
    local args = frame.args;
    
    if args[1] == nil then
        local parent = frame:getParent();
        args = parent.args;
    end
    local min_value = nil;
    
    local i = 1;
    while args[i] ~= nil do
        local val = z._cleanNumber( frame, args[i] );
        if val ~= nil then
            if min_value == nil or val < min_value then
                min_value = val;
            end
        end        
        i = i + 1;
    end
  
    return min_value
end

--[[
average 
 
Finds the average
 
Usage:
    {{#invoke:Math| average | value1 | value2 | ... }}
OR
    {{#invoke:Math| average }}
 
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.average( frame )
    local args = frame.args;
    if args[1] == nil then
        local parent = frame:getParent();
        args = parent.args;
    end
    local sum = 0;
    local count = 0;
 
    local i = 1;
    while args[i] ~= nil do
        local val = z._cleanNumber( frame, args[i] );
        if val ~= nil then
            sum = sum + val
            count = count + 1
        end        
        i = i + 1;
    end
 
    return (count == 0 and 0 or sum/count)
end

--[[
round

Rounds a number to specified precision

Usage:
    {{#invoke:Math | round | value | precision }}
    
--]]
function z.round(frame)
    local value, precision;
    
    value = z._cleanNumber( frame, frame.args[1] or frame.args.value or 0 );
    precision = z._cleanNumber( frame, frame.args[2] or frame.args.precision or 0 );
    
    if value == nil or precision == nil then
        return '<strong class="error">Error de format: els valors han de ser numèrics</strong>'
    else
        return z._round( value, precision );
    end    
end
function z._round( value, precision )
    local rescale = math.pow( 10, precision );
    return math.floor( value * rescale + 0.5 ) / rescale;
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.
    local lang = mw.getContentLanguage();
    
    local value_string, value, precision;
    value, value_string = z._cleanNumber( frame, frame.args[1] or 0 );
    precision = z._cleanNumber( frame, frame.args[2] or 0 );
    
    -- Check for non-numeric input
    if value == nil or precision == nil then
        return '<strong class="error">Error de format: dades no vàlides per arrodonir</strong>'
    end
    
    local current_precision = z._precision( value );

    local order = z._order( value );
    
    -- Due to round-off effects it is neccesary to limit the returned precision under
    -- some circumstances because the terminal digits will be inaccurately reported.
    if order + precision >= 14 then
        orig_precision = z._precision( value_string );
        if order + orig_precision >= 14 then
            precision = 13 - order;        
        end        
    end

    -- If rounding off, truncate extra digits
    if precision < current_precision then
        value = z._round( value, precision );
        current_precision = z._precision( value );
    end    
    
    local formatted_num = lang:formatNum( math.abs(value) );
    local sign;
    
    -- Use proper unary minus sign rather than ASCII default
    if value < 0 then
        sign = '−';
    else
        sign = '';
    end    
        
    -- Handle cases requiring scientific notation
    if string.find( formatted_num, 'E', 1, true ) ~= nil or math.abs(order) >= 9 then
        value = value * math.pow( 10, -order );
        current_precision = current_precision + order;
        precision = precision + order;
        formatted_num = lang:formatNum( math.abs(value) );
    else
        order = 0;        
    end
    formatted_num = sign .. formatted_num;
    
    -- Pad with zeros, if needed    
    if current_precision < precision then
        local padding;
        if current_precision <= 0 then
            if precision > 0 then
                local zero_sep = lang:formatNum( 1.1 );
                formatted_num = formatted_num .. zero_sep:sub(2,2);

                padding = precision;
                if padding > 20 then
                    padding = 20;
                end
                
                formatted_num = formatted_num .. string.rep( '0', padding );
            end            
        else                   
            padding = precision - current_precision
            if padding > 20 then
                padding = 20;
            end
            formatted_num = formatted_num .. string.rep( '0', padding );
        end
    end

    -- Add exponential notation, if necessary.
    if order ~= 0 then
        -- Use proper unary minus sign rather than ASCII default
        if order < 0 then
            order = '−' .. lang:formatNum( math.abs(order) );
        else
            order = lang:formatNum( order );
        end    
        
        formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>'
    end
    
    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:callParserFunction('#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