Module:Utils: Difference between revisions
Made eval_num_arg global (for use in other modules only, not in regular pages) |
log and round can be called from pages or from modules (using _ in the latter case) |
||
Line 1: | Line 1: | ||
local getArgs = require('Module:Arguments').getArgs | |||
local p = {} | local p = {} | ||
Line 18: | Line 19: | ||
-- return logarithm base b of x | -- return logarithm base b of x | ||
function p.log(frame) | function p.log(frame) | ||
-- x defaults to 0 | local args = getArgs(frame) | ||
return p._log(args[1], args[2]) | |||
-- b defaults to 2 ("octave") | end | ||
function p._log(x, b) | |||
-- x defaults to 0 | |||
x = eval_num_arg(x, 0) | |||
-- b defaults to 2 ("octave") | |||
b = eval_num_arg(b, 2) | |||
return math.log(x)/math.log(b) | return math.log(x)/math.log(b) | ||
end | end | ||
Line 27: | Line 33: | ||
-- return x rounded to a precision of p decimal places | -- return x rounded to a precision of p decimal places | ||
function p.round(frame) | function p.round(frame) | ||
-- x defaults to 0 | local args = getArgs(frame) | ||
return p._round(args[1], args[2]) | |||
-- prec defaults to 3 | end | ||
function p._round(x, prec) | |||
-- x defaults to 0 | |||
x = eval_num_arg(x, 0) | |||
-- prec defaults to 3 | |||
prec = eval_num_arg(prec, 3) | |||
return math.floor(x*10^prec+0.5)/10^prec | return math.floor(x*10^prec+0.5)/10^prec | ||
end | end | ||
return p | return p |