Module:Utils: Difference between revisions

Fredg999 (talk | contribs)
Made eval_num_arg global (for use in other modules only, not in regular pages)
Fredg999 (talk | contribs)
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 if omitted
local args = getArgs(frame)
local x = eval_num_arg(frame.args[1], 0)
return p._log(args[1], args[2])
-- b defaults to 2 ("octave") if omitted
end
local b = eval_num_arg(frame.args[2], 2)
 
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 if omitted
local args = getArgs(frame)
local x = eval_num_arg(frame.args[1], 0)
return p._round(args[1], args[2])
-- prec defaults to 3 if omitted
end
local prec = eval_num_arg(frame.args[2], 3)
 
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