fix calculation
preffff>Frozen-mikan (保護ページ編集依頼 による: モジュール:Math/sandbox 2016年3月30日 (水) 12:11 (UTC) の版から複製) |
module:infobox>Primefac (fix calculation) |
||
16行目: | 16行目: | ||
local function err(msg) | local function err(msg) | ||
-- Generates wikitext error messages. | -- Generates wikitext error messages. | ||
return mw.ustring.format('<strong class="error"> | return mw.ustring.format('<strong class="error">Formatting error: %s</strong>', msg) | ||
end | end | ||
219行目: | 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 | ||
244行目: | 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 | ||
296行目: | 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 | ||