Module:Math: Difference between revisions
fix calculation
(صفحهای تازه حاوی «--This module provides a number of basic mathematical operations. local yesno = require('Module:Yesno') local getArgs = require('M...» ایجاد کرد) |
(fix calculation) |
||
Line 5: | Line 5: | ||
]] | ]] | ||
local yesno | local yesno, getArgs -- lazily initialized | ||
local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules. | local p = {} -- Holds functions to be returned from #invoke, and functions to make available to other Lua modules. | ||
Line 17: | Line 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 | ||
Line 50: | Line 49: | ||
end | end | ||
local function | local function fold(func, ...) | ||
-- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters, | -- Use a function on all supplied arguments, and return the result. The function must accept two numbers as parameters, | ||
-- and must return a number as an output. This number is then supplied as input to the next function call. | -- and must return a number as an output. This number is then supplied as input to the next function call. | ||
Line 64: | Line 63: | ||
end | end | ||
return ret, count | return ret, count | ||
end | |||
--[[ | |||
Fold arguments by selectively choosing values (func should return when to choose the current "dominant" value). | |||
]] | |||
local function binary_fold(func, ...) | |||
local value = fold((function(a, b) if func(a, b) then return a else return b end end), ...) | |||
return value | |||
end | end | ||
Line 110: | Line 117: | ||
local input_number = p._cleanNumber(input_string); | local input_number = p._cleanNumber(input_string); | ||
if input_number == nil then | if input_number == nil then | ||
return err(' | return err('order of magnitude input appears non-numeric') | ||
else | else | ||
return p._order(input_number) | return p._order(input_number) | ||
Line 135: | Line 142: | ||
local input_number; | local input_number; | ||
if not yesno then | |||
yesno = require('Module:Yesno') | |||
end | |||
if yesno(trap_fraction, true) then -- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]]. | if yesno(trap_fraction, true) then -- Returns true for all input except nil, false, "no", "n", "0" and a few others. See [[Module:Yesno]]. | ||
local pos = string.find(input_string, '/', 1, true); | local pos = string.find(input_string, '/', 1, true); | ||
Line 150: | Line 160: | ||
input_number, input_string = p._cleanNumber(input_string); | input_number, input_string = p._cleanNumber(input_string); | ||
if input_string == nil then | if input_string == nil then | ||
return err(' | return err('precision input appears non-numeric') | ||
else | else | ||
return p._precision(input_string) | return p._precision(input_string) | ||
Line 188: | Line 198: | ||
return result | return result | ||
end | end | ||
--[[ | --[[ | ||
Line 205: | Line 216: | ||
function p._max(...) | function p._max(...) | ||
local function | local max_value = binary_fold((function(a, b) return a > b end), ...) | ||
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 237: | Line 272: | ||
function p._min(...) | function p._min(...) | ||
local function | local min_value = binary_fold((function(a, b) return a < b end), ...) | ||
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 268: | Line 322: | ||
function p._average(...) | function p._average(...) | ||
local function | local sum, count = fold((function(a, b) return a + b end), ...) | ||
if not sum then | if not sum then | ||
return 0 | return 0 | ||
Line 293: | Line 344: | ||
local precision = p._cleanNumber(args[2] or args.precision or 0) | local precision = p._cleanNumber(args[2] or args.precision or 0) | ||
if value == nil or precision == nil then | if value == nil or precision == nil then | ||
return err(' | return err('round input appears non-numeric') | ||
else | else | ||
return p._round(value, precision) | return p._round(value, precision) | ||
Line 302: | 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 | ||
Line 318: | Line 382: | ||
local y = p._cleanNumber(args[2]) | local y = p._cleanNumber(args[2]) | ||
if not x then | if not x then | ||
return err(' | return err('first argument to mod appears non-numeric') | ||
elseif not y then | elseif not y then | ||
return err(' | return err('second argument to mod appears non-numeric') | ||
else | else | ||
return p._mod(x, y) | return p._mod(x, y) | ||
Line 360: | Line 424: | ||
return oldr | return oldr | ||
end | end | ||
local result, count = | local result, count = fold(findGcd, ...) | ||
return result | return result | ||
end | end | ||
Line 390: | Line 454: | ||
-- Check for non-numeric input | -- Check for non-numeric input | ||
if value == nil or precision == nil then | if value == nil or precision == nil then | ||
return err(' | return err('invalid input when rounding') | ||
end | end | ||
Line 465: | Line 529: | ||
end | end | ||
formatted_num = formatted_num .. '<span style="margin:0 . | formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>' | ||
end | end | ||
Line 491: | Line 555: | ||
-- If failed, attempt to evaluate input as an expression | -- If failed, attempt to evaluate input as an expression | ||
if number == nil then | if number == nil then | ||
local | local success, result = pcall(mw.ext.ParserFunctions.expr, number_string) | ||
if success then | |||
number = tonumber(result) | |||
if | |||
number = | |||
number_string = tostring(number) | number_string = tostring(number) | ||
else | else | ||
Line 518: | Line 580: | ||
]] | ]] | ||
local function | local mt = { __index = function(t, k) | ||
return function (frame) | return function(frame) | ||
if not getArgs then | |||
getArgs = require('Module:Arguments').getArgs | |||
end | |||
return wrap[k](getArgs(frame)) -- Argument processing is left to Module:Arguments. Whitespace is trimmed and blank arguments are removed. | |||
end | end | ||
end | end } | ||
return p | return setmetatable(p, mt) |