Changes

Jump to navigation Jump to search
1,015 bytes added ,  04:57, 1 October 2018
fix calculation
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 296: Line 353:  
local rescale = math.pow(10, precision or 0);
 
local rescale = math.pow(10, precision or 0);
 
return math.floor(value * rescale + 0.5) / rescale;
 
return math.floor(value * rescale + 0.5) / rescale;
 +
end
 +
 +
--[[
 +
log10
 +
 +
returns the log (base 10) of a number
 +
 +
Usage:
 +
{{#invoke:Math | log10 | x }}
 +
]]
 +
 +
function wrap.log10(args)
 +
return math.log10(args[1])
 
end
 
end
  
Anonymous user

Navigation menu