| Modul Lua ini digunakan pada sekitar 167.000 halaman. Untuk mencegah suntingan mengganggu skala besar dan beban peladen, setiap perubahan harus diuji di subhalaman /bak pasir atau /kasus uji Modul:Math, atau bak pasir modul Anda. Perubahan yang telah diuji kemudian dapat ditambahkan ke halaman ini dalam satu suntingan. Pertimbangkan untuk mendiskusikan perubahan di halaman pembicaraan sebelum mengimplementasikannya. |
| Modul ini dilindungi. Modul ini sangat mencolok yang digunakan oleh banyak halaman, atau sangat sering disubstitusikan. Karena vandalisme atau kesalahan akan mempengaruhi banyak halaman, dan suntingan kecil dapat memberi beban besar pada server, modul ini dilindungi dari penyuntingan. |
| Modul ini menggunakan Lua: |
Penggunaan dari modul Lua lainnyaPenggunaan dari modul Lua lainnya
Untuk menggunakan modul dari halaman wiki pada umumnya, tidak diperlukan persiapan khusus. Jika Anda menggunakan modul dari modul Lua lain, Anda perlu memuatnya terlebih dahulu, seperti ini:
localmm=require('Module:Math')
(Variabel mm merupakan singkatan dari Module Math; Anda bisa memilih sesuatu yang lebih deskriptif jika Anda mau.)
Sebagian besar fungsi dalam modul ini memiliki versi untuk Lua dan versi untuk #invoke. Dimungkinkan untuk menggunakan fungsi #invoke dari modul Lua lainnya, namun, menggunakan fungsi Lua memiliki keuntungan karena Anda tidak perlu mengakses objek frame Lua frame object. Fungsi Lua diawali dengan _, sedangkan fungsi #invoke tidak.
randomrandom
{{#invoke:math|random}}
{{#invoke:math|random|max_value}}
{{#invoke:math|random|min_value|max_value}}
mm._random() mm._random(max_value) mm._random(min_value,max_value)
Menghasilkan angka acak.
- Jika tidak ada argumen yang ditentukan, angka yang dihasilkan lebih besar dari atau sama dengan 0 dan kurang dari 1.
- Jika satu argumen diberikan, angka yang dihasilkan adalah bilangan bulat antara 1 dan argumen tersebut. Argumen harus berupa bilangan bulat positif.
- Jika dua argumen diberikan, angka yang dihasilkan adalah bilangan bulat antara argumen pertama dan kedua. Kedua argumen harus berupa bilangan bulat, tetapi boleh negatif.
Fungsi ini tidak akan bekerja dengan benar untuk angka kurang dari −232 dan lebih besar dari 232 − 1. Jika Anda perlu menggunakan angka di luar rentang ini, disarankan agar Anda menggunakan Modul:Random.
orderorder
{{#invoke:math|order|n}}
mm._order(n)
Menentukan tingkat besaran suatu angka.
Penyunting dapat melakukan uji coba pada halaman bak pasir (buat | cermin) dan kasus uji (buat) modul ini.
Subhalaman modul ini.
--[[ This module provides a number of basic mathematical operations. ]] localyesno,getArgs-- lazily initialized localp={}-- Holds functions to be returned from #invoke, and functions to make available to other Lua modules. localwrap={}-- Holds wrapper functions that process arguments from #invoke. These act as intemediary between functions meant for #invoke and functions meant for Lua. --[[ Helper functions used to avoid redundant code. ]] localfunctionerr(msg) -- Generates wikitext error messages. returnmw.ustring.format('<strong class="error">Formatting error: %s</strong>',msg) end localfunctionunpackNumberArgs(args) -- Returns an unpacked list of arguments specified with numerical keys. localret={} fork,vinpairs(args)do iftype(k)=='number'then table.insert(ret,v) end end returnunpack(ret) end localfunctionmakeArgArray(...) -- Makes an array of arguments from a list of arguments that might include nils. localargs={...}-- Table of arguments. It might contain nils or non-number values, so we can't use ipairs. localnums={}-- Stores the numbers of valid numerical arguments. localret={} fork,vinpairs(args)do v=p._cleanNumber(v) ifvthen nums[#nums+1]=k args[k]=v end end table.sort(nums) fori,numinipairs(nums)do ret[#ret+1]=args[num] end returnret end localfunctionfold(func,...) -- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters, -- and must return a number as an output. This number is then supplied as input to the next function call. localvals=makeArgArray(...) localcount=#vals-- The number of valid arguments ifcount==0thenreturn -- Exit if we have no valid args, otherwise removing the first arg would cause an error. nil,0 end localret=table.remove(vals,1) for_,valinipairs(vals)do ret=func(ret,val) end returnret,count end --[[ Fold arguments by selectively choosing values (func should return when to choose the current "dominant" value). ]] localfunctionbinary_fold(func,...) localvalue=fold((function(a,b)iffunc(a,b)thenreturnaelsereturnbendend),...) returnvalue end --[[ random Generate a random number Usage: {{#invoke: Math | random }} {{#invoke: Math | random | maximum value }} {{#invoke: Math | random | minimum value | maximum value }} ]] functionwrap.random(args) localfirst=p._cleanNumber(args[1]) localsecond=p._cleanNumber(args[2]) returnp._random(first,second) end functionp._random(first,second) math.randomseed(mw.site.stats.edits+mw.site.stats.pages+os.time()+math.floor(os.clock()*1000000000)) -- math.random will throw an error if given an explicit nil parameter, so we need to use if statements to check the params. iffirstandsecondthen iffirst<=secondthen-- math.random doesn't allow the first number to be greater than the second. returnmath.random(first,second) end elseiffirstthen returnmath.random(first) else returnmath.random() end end --[[ order Determine order of magnitude of a number Usage: {{#invoke: Math | order | value }} ]] functionwrap.order(args) localinput_string=(args[1]orargs.xor'0'); localinput_number=p._cleanNumber(input_string); ifinput_number==nilthen returnerr('Masukan urutan besaran tampak non-numerik') else returnp._order(input_number) end end functionp._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 }} ]] functionwrap.precision(args) localinput_string=(args[1]orargs.xor'0'); localtrap_fraction=args.check_fraction; ifnotyesnothen yesno=require('Module:Yesno') end ifyesno(trap_fraction,true)then-- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]]. 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 local_,clean_string=p._cleanNumber(input_string); ifclean_string==nilthen returnerr('Input presisi tampak non-numerik') else returnp._precision(clean_string) end end functionp._precision(x) iftype(x)=='number'then x=tostring(x) end localresult=0; --subtract exponent local_,_,coefficient,exponent=x:find("^(.*)[eE](.*)$") ifcoefficientthen result=result-(tonumber(exponent)or0) x=coefficient end --add decimal places locali,j=x:find('%..*$') ifithen result=result+(j-i) returnresult end --subtract trailing zeroes i,j=x:find('[^0]0+$') ifithen result=result-(j-i) end returnresult end --[[ max Finds the maximum argument Usage: {{#invoke:Math| max | value1 | value2 | ... }} Note, any values that do not evaluate to numbers are ignored. ]] functionwrap.max(args) returnp._max(unpackNumberArgs(args)) end functionp._max(...) localmax_value=binary_fold((function(a,b)returna>bend),...) ifmax_valuethen returnmax_value end end --[[ median Find the median of set of numbers Usage: {{#invoke:Math | median | number1 | number2 | ...}} OR {{#invoke:Math | median }} ]] functionwrap.median(args) returnp._median(unpackNumberArgs(args)) end functionp._median(...) localvals=makeArgArray(...) localcount=#vals table.sort(vals) ifcount==0then return0 end ifp._mod(count,2)==0then return(vals[count/2]+vals[count/2+1])/2 else returnvals[math.ceil(count/2)] end 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. ]] functionwrap.min(args) returnp._min(unpackNumberArgs(args)) end functionp._min(...) localmin_value=binary_fold((function(a,b)returna<bend),...) ifmin_valuethen returnmin_value end end --[[ sum Finds the sum Usage: {{#invoke:Math| sum | value1 | value2 | ... }} OR {{#invoke:Math| sum }} Note, any values that do not evaluate to numbers are ignored. ]] functionwrap.sum(args) returnp._sum(unpackNumberArgs(args)) end functionp._sum(...) localsums=fold((function(a,b)returna+bend),...) ifnotsumsthen return0 else returnsums end end --[[ average Finds the average Usage: {{#invoke:Math| average | value1 | value2 | ... }} OR {{#invoke:Math| average }} Note, any values that do not evaluate to numbers are ignored. ]] functionwrap.average(args) returnp._average(unpackNumberArgs(args)) end functionp._average(...) localsum,count=fold((function(a,b)returna+bend),...) ifnotsumthen return0 else returnsum/count end end --[[ round Rounds a number to specified precision Usage: {{#invoke:Math | round | value | precision }} --]] functionwrap.round(args) localvalue=p._cleanNumber(args[1]orargs.valueor0) localprecision=p._cleanNumber(args[2]orargs.precisionor0) ifvalue==nilorprecision==nilthen returnerr('Masukan pembulatan tampak non-numerik') else returnp._round(value,precision) end end functionp._round(value,precision) localrescale=math.pow(10,precisionor0); returnmath.floor(value*rescale+0.5)/rescale; end --[[ log10 returns the log (base 10) of a number Usage: {{#invoke:Math | log10 | x }} ]] functionwrap.log10(args) returnmath.log10(args[1]) end --[[ mod Implements the modulo operator Usage: {{#invoke:Math | mod | x | y }} --]] functionwrap.mod(args) localx=p._cleanNumber(args[1]) localy=p._cleanNumber(args[2]) ifnotxthen returnerr('Argumen pertama untuk mod tampak bukan berupa numnerik') elseifnotythen returnerr('Argumen kedua untuk mod tampak bukan berupa numnerik') else returnp._mod(x,y) end end functionp._mod(x,y) localret=x%y ifnot(0<=retandret<y)then ret=0 end returnret end --[[ gcd Calculates the greatest common divisor of multiple numbers Usage: {{#invoke:Math | gcd | value 1 | value 2 | value 3 | ... }} --]] functionwrap.gcd(args) returnp._gcd(unpackNumberArgs(args)) end functionp._gcd(...) localfunctionfindGcd(a,b) localr=b localoldr=a whiler~=0do localquotient=math.floor(oldr/r) oldr,r=r,oldr-quotient*r end ifoldr<0then oldr=oldr*-1 end returnoldr end localresult=fold(findGcd,...) returnresult 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 }} ]] functionwrap.precision_format(args) localvalue_string=args[1]or0 localprecision=args[2]or0 returnp._precision_format(value_string,precision) end functionp._precision_format(value_string,precision) -- For access to Mediawiki built-in formatter. locallang=mw.getContentLanguage(); localvalue value,value_string=p._cleanNumber(value_string) precision=p._cleanNumber(precision) -- Check for non-numeric input ifvalue==nilorprecision==nilthen returnerr('Masukan tidak valid saat pembulatan') end localabs_value=math.abs(value) localcurrent_precision=p._precision(value) localorder=p._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 iforder+p._precision(value_string)>=14then precision=13-order; end end -- If rounding off, truncate extra digits ifprecision<current_precisionthen value=p._round(value,precision) abs_value=math.abs(value) current_precision=p._precision(value) end localformatted_num=lang:formatNum(abs_value) -- Handle cases requiring scientific notation ifmath.abs(order)>=9orstring.find(formatted_num,'E',1,true)then value=value*(10^(-order)) abs_value=math.abs(value) current_precision=current_precision+order precision=precision+order formatted_num=lang:formatNum(abs_value) else order=0; end -- Use proper unary minus sign rather than ASCII default formatted_num=((value<0)and'−'or'')..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 --[[ divide Implements the division operator Usage: {{#invoke:Math | divide | x | y | round= | precision= }} --]] functionwrap.divide(args) localx=args[1] localy=args[2] localround=args.round localprecision=args.precision ifnotyesnothen yesno=require('Module:Yesno') end returnp._divide(x,y,yesno(round),precision) end functionp._divide(x,y,round,precision) ify==nilory==""then returnerr("Empty divisor") elseifnottonumber(y)then iftype(y)=='string'andstring.sub(y,1,1)=='<'then returny else returnerr("Not a number: "..y) end elseifx==nilorx==""then returnerr("Empty dividend") elseifnottonumber(x)then iftype(x)=='string'andstring.sub(x,1,1)=='<'then returnx else returnerr("Not a number: "..x) end else localz=x/y ifroundthen returnp._round(z,0) elseifprecisionthen returnp._round(z,precision) else returnz end end 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. ]] functionp._cleanNumber(number_string,unformat) 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, try unformatting the number_string ifunformatandnumber==nilthen -- Cannot use mw.language.getContentLanguage():parseFormattedNumber() -- because, unlike {{formatnum:|R}}, it won't ignore operators (it -- would return an error on "1,234+1,234" instead of "1234+1234") number_string=mw.getCurrentFrame():callParserFunction("formatnum",{number_string,"R"}) number=tonumber(number_string) end -- 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 --[[ Wrapper function that does basic argument processing. This ensures that all functions from #invoke can use either the current frame or the parent frame, and it also trims whitespace for all arguments and removes blank arguments. ]] localmt={__index=function(_,k) returnfunction(frame) ifnotgetArgsthen getArgs=require('Module:Arguments').getArgs end returnwrap[k](getArgs(frame))-- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed. end end} returnsetmetatable(p,mt)