Modul:Math

Vikipediya, azad ensiklopediya
Naviqasiyaya keç Axtarışa keç

Bu modul bir qədər riyazi funksiyaya malikdir.


Funksiyaları

random

{{#invoke:math|random}}
{{#invoke:math|random|A}}
{{#invoke:math|random|A|B}}

max

min

Npmrəli maksimal və minimal parametrlərdən #invoke,

round

valueilk parametri ikinci rəqəmə qədər yuvarlaqlaşdırır və yaprecision,0.5 ən çoxu 1-ə, ən azı 0-a qədər.

order

{{#invoke:math|order|A}}


precision

Rəqəm dəqiqliyi.

precision_format

«·10x».

Roman

1-dən 4999999 qədərRum rəqəmləriüçün.

_cleanNumber(frame,x)

_order(n)

Rəqəm sırası n.

_precision(x)

Rəqəm dəqiqliyi.

_round(value, precision)

Yuvarlaqlaşdırma


--[[

This module provides a number of basic mathematical operations.

]]
localz={}

-- Generate random number
functionz.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])

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

--[[
order

Determine order of magnitude of a number

Usage:
{{#invoke: Math | order | value }}
]]
functionz.order(frame)
localinput_string=(frame.args[1]orframe.args.xor'0');
localinput_number;

input_number=z._cleanNumber(frame,input_string);
ifinput_number==nilthen
return'<strong class= "error" >Məlumat formatı səhvi: onluq dərəcəni təyin edərkən rəqəm olmayan giriş dəyəri</strong>'
else
returnz._order(input_number)
end
end
functionz._order(x)
ifx==0thenreturn0end
returnmath.floor(math.log10(math.abs(x)))
end

--[[
precision

Detemines the precision of a number using the string representation

Usage:
{{ #invoke: Math | precision | value }}
]]
functionz.precision(frame)
localinput_string=(frame.args[1]orframe.args.xor'0');
localtrap_fraction=frame.args.check_fractionorfalse;
localinput_number;

iftype(trap_fraction)=='string'then
trap_fraction=trap_fraction:lower();
iftrap_fraction=='false'ortrap_fraction=='0'or
trap_fraction=='no'ortrap_fraction==''then
trap_fraction=false;
else
trap_fraction=true;
end
end

iftrap_fractionthen
localpos=string.find(input_string,'/',1,true);
ifpos~=nilthen
ifstring.find(input_string,'/',pos+1,true)==nilthen
localdenominator=string.sub(input_string,pos+1,-1);
localdenom_value=tonumber(denominator);
ifdenom_value~=nilthen
returnmath.log10(denom_value);
end
end
end
end

input_number,input_string=z._cleanNumber(frame,input_string);
ifinput_string==nilthen
return'<strong class= "error" >Məlumat formatında səhv: kəsrli hissəni müəyyən edərkən rəqəm olmayan giriş dəyəri</strong>'
else
returnz._precision(input_string)
end
end
functionz._precision(x)
x=string.upper(x)

localdecimal=string.find(x,'[.,]',1)
localexponent_pos=string.find(x,'E',1,true)
localresult=0;

ifexponent_pos~=nilthen
localexponent=string.sub(x,exponent_pos+1)
x=string.sub(x,1,exponent_pos-1)
result=result-tonumber(exponent)
end

ifdecimal~=nilthen
result=result+string.len(x)-decimal
returnresult
end

localpos=string.len(x);
whilex:byte(pos)==string.byte('0')do
pos=pos-1
result=result-1
ifpos<=0then
return0
end
end

returnresult
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.
]]
functionz.max(frame)
localargs=frame.args;

ifargs[1]==nilthen
localparent=frame:getParent();
args=parent.args;
end
localmax_value=nil;

locali=1;
whileargs[i]~=nildo
localval=z._cleanNumber(frame,args[i]);
ifval~=nilthen
ifmax_value==nilorval>max_valuethen
max_value=val;
end
end
i=i+1;
end

returnmax_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.
]]
functionz.min(frame)
localargs=frame.args;

ifargs[1]==nilthen
localparent=frame:getParent();
args=parent.args;
end
localmin_value=nil;

locali=1;
whileargs[i]~=nildo
localval=z._cleanNumber(frame,args[i]);
ifval~=nilthen
ifmin_value==nilorval<min_valuethen
min_value=val;
end
end
i=i+1;
end

returnmin_value
end

--[[
round

Rounds a number to specified precision

Usage:
{{#invoke:Math | round | value | precision }}

--]]
functionz.round(frame)
localvalue,precision;

value=z._cleanNumber(frame,frame.args[1]orframe.args.valueor0);
precision=z._cleanNumber(frame,frame.args[2]orframe.args.precisionor0);

ifvalue==nilorprecision==nilthen
return'<strong class= "error" >Məlumat formatında səhv: Tam ədədə yuvarlaqlaşdırarkən rəqəm olmayan giriş dəyəri</strong>'
else
returnz._round(value,precision);
end
end
functionz._round(value,precision)
localrescale=math.pow(10,precision);
returnmath.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 }}
]]

functionz.precision_format(args)
localvalue_string=args[1]or0
localprecision=args[2]or0
returnz._precision_format(value_string,precision)
end

functionz._cleanNumberNew(number_string)
iftype(number_string)=='number'then
-- We were passed a number, so we don't need to do any processing.
returnnumber_string,tostring(number_string)
elseiftype(number_string)~='string'ornotnumber_string:find('%S')then
-- We were passed a non-string or a blank string, so exit.
returnnil,nil;
end

-- Attempt basic conversion
localnumber=tonumber(number_string)

-- If failed, attempt to evaluate input as an expression
ifnumber==nilthen
localsuccess,result=pcall(mw.ext.ParserFunctions.expr,number_string)
ifsuccessthen
number=tonumber(result)
number_string=tostring(number)
else
number=nil
number_string=nil
end
else
number_string=number_string:match("^%s*(.-)%s*$")-- String is valid but may contain padding, clean it.
number_string=number_string:match("^%+(.*)$")ornumber_string-- Trim any leading + signs.
ifnumber_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

returnnumber,number_string
end

functionz._precision_format(value_string,precision)
-- For access to Mediawiki built-in formatter.
locallang=mw.getContentLanguage();

localr="before: v ="..tostring(value_string)..";p="..tostring(precision)..'\n'

localvalue
value,value_string=z._cleanNumberNew(value_string)
precision=z._cleanNumberNew(precision)

-- Check for non-numeric input
ifvalue==nilorprecision==nilthen
return'<strong class= "error" >Məlumat formatında səhv: müəyyən bir doğruluqda yuvarlaqlaşdırarkən rəqəm olayan giriş dəyəri</strong>'
end

localcurrent_precision=z._precision(value)

localorder=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.
iforder+precision>=14then
localorig_precision=z._precision(value_string)
iforder+orig_precision>=14then
precision=13-order;
end
end

-- If rounding off, truncate extra digits
ifprecision<current_precisionthen
value=z._round(value,precision)
current_precision=z._precision(value)
end

localformatted_num=lang:formatNum(math.abs(value))
localsign

-- Use proper unary minus sign rather than ASCII default
ifvalue<0then
sign='−'
else
sign=''
end

-- Handle cases requiring scientific notation
ifstring.find(formatted_num,'E',1,true)~=nilormath.abs(order)>=9then
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
ifcurrent_precision<precisionthen
localpadding
ifcurrent_precision<=0then
ifprecision>0then
localzero_sep=lang:formatNum(1.1)
formatted_num=formatted_num..zero_sep:sub(2,2)

padding=precision
ifpadding>20then
padding=20
end

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

-- Add exponential notation, if necessary.
iforder~=0then
-- Use proper unary minus sign rather than ASCII default
iforder<0then
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

returnformatted_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.
]]

functionz._cleanNumber(frame,number_string)
ifnumber_string==nilornumber_string:len()==0then
returnnil,nil;
end

-- Attempt basic conversion
localnumber=tonumber(number_string)

-- If failed, attempt to evaluate input as an expression
ifnumber==nilthen
localattempt=frame:callParserFunction('#expr',number_string);
attempt=tonumber(attempt);
ifattempt~=nilthen
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=mw.text.trim(number_string)
end

returnnumber,number_string;
end

localfunctionroman(i)
localw,t,val,let={},{
{1000,"M"},
{900,"CM"},
{500,"D"},
{400,"CD"},
{100,"C"},
{90,"XC"},
{50,"L"},
{40,"XL"},
{10,"X"},
{9,"IX"},
{5,"V"},
{4,"IV"},
{1,"I"}
}
forn,vinipairs(t)do
val,let=unpack(v)
w[n]=string.rep(let,i/val)
i=i%val
end
returntable.concat(w)
end

functionz.Roman(frame)-- Преобразует числа от 1 до 4999999 в римские
localfunctiontry_tonumber(a)
returnmath.floor(tonumber(a)orerror('\''..a..'\'не является числом.'));
end
localstr=frame.args[1]or'';
ifstr==''then-- boş parametr
returnstr;
end
localr,N=pcall(try_tonumber,str);
ifrthen
ifN<1orN>=5e6then
returnframe.args[2]or'N/A'
end
localR=N%5000
N=(N-R)/1000
return(N>0andtable.concat{'<span style= "text-decoration:overline;" >',roman(N),'</span>'}or'')..roman(R)
else
return'<strong class= "error" >'..N..'</strong>';
end
end

functionz.AzerbaijaniFem(frame)
localresult=z.Azerbaijani(frame)
returnstring.gsub(string.gsub(result,'iki$','iki'),'bir$','')
end

functionz.Azerbaijani(frame)
localstr=frame.args[1]or'';
ifstr==''then
return'';
end
locallang=mw.getLanguage('az')
ifstring.find(str,'%.')orstring.find(str,',')then
a,b=string.match(str,'(%d+).(%d+)')
localbasic={'on','yüz'}
localprefixes={'','десяти','сто'}
localorders={'тысячн','миллионн','миллиардн',
'триллионн','квадриллионн'}
localed={'ая','ые','ых'}
a=#aandaor'0'
b=#bandbor'0'
localorder=math.floor(#b/#prefixes)
iforder>#ordersthen
order=#orders
b=string.sub(b,0,#prefixes*#orders)
end
mw.log(order)
localia=z.AzerbaijaniFem({args={a}})
localib=z.AzerbaijaniFem({args={b}})
localintgr='цел'
localfract=''
iforder==0then
fract=basic[#b]
else
fract=prefixes[#b%#prefixes+1]..orders[order]
end
intgr=lang:plural(tonumber(a,10)%1000,intgr..ed[1],
intgr..ed[2],
intgr..ed[3])
fract=lang:plural(tonumber(b,10)%1000,fract..ed[1],
fract..ed[2],
fract..ed[3])
returnia..' '..intgr..' и '..ib..' '..fract
end
localnumber=tonumber(str,10)

localzero='sıfır'
localones={'bir','iki','üç','dörd','beş','altı',
'yeddi','səkkiz','doqquz','on','on bir','on iki',
'on üç','on dörd','on beş','on altı','on yeddi',
'on səkkiz','on doqquz'}
localtens={'','iyirmi','otuz','qırx','əlli',
'altmış','yetmiş','səksən','doxsan'}
localhundreds={'yüz','iki yüz','üç yüz','dörd yüz','beş yüz',
'altı yüz','yeddi yüz','səkkiz yüz','doqquz yüz'}

localunitsPlural={
{'','',''},
{'min','min','min'},
{'milyon','milyon','milyon'},
{'milyard','milyard','milyard'},
{'trilyon','trilyon','trilyon'},
}

localout=''
localoutMinus=''

if(number<0)then
outMinus='mənfi '
number=math.abs(number)
end

localtripletPos=0
while(number>0)do
localtriplet=number%1000
number=math.floor(number/1000)

tripletPos=tripletPos+1
if(tripletPos>5)then
return''
end

localtripletStr=''
localtripletUnit=''
if(triplet>0)then
localunitPlural=unitsPlural[tripletPos]
tripletUnit=lang:plural(triplet,unitPlural[1],unitPlural[2],unitPlural[3])
end

if(triplet>=100)then
tripletStr=hundreds[math.floor(triplet/100)]
triplet=triplet%100
end

if(triplet>=20)then
tripletStr=tripletStr..' '..tens[math.floor(triplet/10)]
triplet=triplet%10
end

if(triplet>=1)then
tripletStr=tripletStr..' '..ones[triplet]
end

if(tripletPos==2)then
tripletStr=mw.ustring.gsub(tripletStr,'bir$','')
tripletStr=mw.ustring.gsub(tripletStr,'iki$','iki')
end

out=tripletStr..' '..tripletUnit..' '..out
end

if(out=='')then
out=zero
end

out=outMinus..out
out=mw.ustring.gsub(out,' +',' ')
out=mw.text.trim(out)
returnout
end

returnz