Module:Math: Difference between revisions

From Chalo Chatu, Zambia online encyclopedia
m
1 revision imported
m (1 revision imported)
 
m (1 revision imported)
(One intermediate revision by the same user not shown)
Line 219: Line 219:
if max_value then
if max_value then
return max_value
return max_value
end
end
--[[
median
Find the median of set of numbers
Usage:
{{#invoke:Math | median | number1 | number2 | ...}}
OR
{{#invoke:Math | median }}
]]
function wrap.median(args)
return p._median(unpackNumberArgs(args))
end
function p._median(...)
local vals = makeArgArray(...)
local count = #vals
table.sort(vals)
if count == 0 then
return 0
end
if p._mod(count, 2) == 0 then
return (vals[count/2] + vals[count/2+1])/2
else
return vals[math.ceil(count/2)]
end
end
end
end
Line 244: Line 275:
if min_value then
if min_value then
return min_value
return min_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.
]]
function wrap.sum(args)
return p._sum(unpackNumberArgs(args))
end
function p._sum(...)
local sums, count = fold((function(a, b) return a + b end), ...)
if not sums then
return 0
else
return sums
end
end
end
end
Line 304: Line 361:


Usage:
Usage:
{{#invoke:Math | log | x }}
{{#invoke:Math | log10 | x }}
]]
]]


Administrators, upwizcampeditors
0

edits