کاربر ناشناس
پودمان:Math: تفاوت میان نسخهها
تبدیل همه ورودیها به انگلیسی
جز (۱ نسخه واردشده) |
(تبدیل همه ورودیها به انگلیسی) |
||
خط ۱: | خط ۱: | ||
local numConv = require("module:Numeral converter").convert | |||
--[[ | --[[ | ||
خط ۵: | خط ۶: | ||
]] | ]] | ||
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. | ||
خط ۲۵: | خط ۲۵: | ||
for k, v in pairs(args) do | for k, v in pairs(args) do | ||
if type(k) == 'number' then | if type(k) == 'number' then | ||
v = numConv('en',v) | |||
table.insert(ret, v) | table.insert(ret, v) | ||
end | end | ||
خط ۵۰: | خط ۵۱: | ||
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. | ||
local vals = makeArgArray(...) | local vals = makeArgArray(...) | ||
local count = #vals -- The number of valid arguments | local count = #vals -- The number of valid arguments | ||
if count == 0 then return | if count == 0 then return | ||
-- Exit if we have no valid args, otherwise removing the first arg would cause an error. | -- Exit if we have no valid args, otherwise removing the first arg would cause an error. | ||
nil, 0 | nil, 0 | ||
end | end | ||
local ret = table.remove(vals, 1) | local ret = table.remove(vals, 1) | ||
for _, val in ipairs(vals) do | for _, val in ipairs(vals) do | ||
خط ۶۴: | خط ۶۵: | ||
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 | ||
خط ۱۱۰: | خط ۱۱۹: | ||
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('ورودی مرتبه بزرگی بنظر میآید غیرعددی باشد') | ||
else | else | ||
return p._order(input_number) | return p._order(input_number) | ||
end | end | ||
end | end | ||
خط ۱۳۵: | خط ۱۴۴: | ||
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); | ||
خط ۱۴۴: | خط ۱۵۶: | ||
return math.log10(denom_value); | return math.log10(denom_value); | ||
end | end | ||
end | end | ||
end | end | ||
end | end | ||
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('ورودی دقت بنظر میآید غیرعددی باشد') | ||
else | else | ||
return p._precision(input_string) | return p._precision(input_string) | ||
end | end | ||
end | end | ||
خط ۱۷۰: | خط ۱۸۲: | ||
x = string.sub(x, 1, exponent_pos - 1) | x = string.sub(x, 1, exponent_pos - 1) | ||
result = result - tonumber(exponent) | result = result - tonumber(exponent) | ||
end | end | ||
if decimal ~= nil then | if decimal ~= nil then | ||
خط ۱۸۸: | خط ۲۰۰: | ||
return result | return result | ||
end | end | ||
--[[ | --[[ | ||
خط ۲۰۵: | خط ۲۱۸: | ||
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 | ||
خط ۲۱۹: | خط ۲۲۵: | ||
--[[ | --[[ | ||
min | 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 | |||
--[[ | |||
min | |||
Finds the minimum argument | Finds the minimum argument | ||
خط ۲۳۷: | خط ۲۷۴: | ||
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 | ||
خط ۲۵۱: | خط ۲۸۱: | ||
--[[ | --[[ | ||
average | 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 | |||
--[[ | |||
average | |||
Finds the average | Finds the average | ||
خط ۲۶۸: | خط ۳۲۴: | ||
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 | ||
خط ۲۹۳: | خط ۳۴۶: | ||
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('ورودی گرد بنظر میآید غیرعددی باشد') | ||
else | else | ||
return p._round(value, precision) | return p._round(value, precision) | ||
end | end | ||
end | end | ||
خط ۳۰۲: | خط ۳۵۵: | ||
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 | ||
خط ۳۱۸: | خط ۳۸۴: | ||
local y = p._cleanNumber(args[2]) | local y = p._cleanNumber(args[2]) | ||
if not x then | if not x then | ||
return err('نخستین آرگومان برای mod بنظر میآید | return err('نخستین آرگومان برای mod بنظر میآید غیرعددی باشد') | ||
elseif not y then | elseif not y then | ||
return err('دومین آرگومان برای mod بنظر میآید | return err('دومین آرگومان برای mod بنظر میآید غیرعددی باشد') | ||
else | else | ||
return p._mod(x, y) | return p._mod(x, y) | ||
end | end | ||
end | end | ||
خط ۳۶۰: | خط ۴۲۶: | ||
return oldr | return oldr | ||
end | end | ||
local result, count = | local result, count = fold(findGcd, ...) | ||
return result | return result | ||
end | end | ||
خط ۳۶۷: | خط ۴۳۳: | ||
precision_format | precision_format | ||
Rounds a number to the specified precision and formats according to rules | Rounds a number to the specified precision and formats according to rules | ||
originally used for {{template:Rnd}}. Output is a string. | originally used for {{template:Rnd}}. Output is a string. | ||
خط ۳۹۹: | خط ۴۶۵: | ||
-- some circumstances because the terminal digits will be inaccurately reported. | -- some circumstances because the terminal digits will be inaccurately reported. | ||
if order + precision >= 14 then | if order + precision >= 14 then | ||
if order + p._precision(value_string) >= 14 then | |||
precision = 13 - order; | |||
precision = 13 - order; | end | ||
end | |||
end | end | ||
خط ۴۰۹: | خط ۴۷۴: | ||
value = p._round(value, precision) | value = p._round(value, precision) | ||
current_precision = p._precision(value) | current_precision = p._precision(value) | ||
end | end | ||
local formatted_num = lang:formatNum(math.abs(value)) | local formatted_num = lang:formatNum(math.abs(value)) | ||
خط ۴۱۹: | خط ۴۸۴: | ||
else | else | ||
sign = '' | sign = '' | ||
end | end | ||
-- Handle cases requiring scientific notation | -- Handle cases requiring scientific notation | ||
خط ۴۲۸: | خط ۴۹۳: | ||
formatted_num = lang:formatNum(math.abs(value)) | formatted_num = lang:formatNum(math.abs(value)) | ||
else | else | ||
order = 0; | order = 0; | ||
end | end | ||
formatted_num = sign .. formatted_num | formatted_num = sign .. formatted_num | ||
-- Pad with zeros, if needed | -- Pad with zeros, if needed | ||
if current_precision < precision then | if current_precision < precision then | ||
local padding | local padding | ||
خط ۴۴۶: | خط ۵۱۱: | ||
formatted_num = formatted_num .. string.rep('0', padding) | formatted_num = formatted_num .. string.rep('0', padding) | ||
end | end | ||
else | else | ||
padding = precision - current_precision | padding = precision - current_precision | ||
if padding > 20 then | if padding > 20 then | ||
خط ۴۶۳: | خط ۵۲۸: | ||
else | else | ||
order = lang:formatNum(order) | order = lang:formatNum(order) | ||
end | end | ||
formatted_num = formatted_num .. '<span style="margin:0 . | formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>۱۰<sup>' .. order .. '</sup>' | ||
end | end | ||
خط ۴۷۲: | خط ۵۳۷: | ||
--[[ | --[[ | ||
Helper function that interprets the input numerically. If the | Helper function that interprets the input numerically. If the | ||
input does not appear to be a number, attempts evaluating it as | input does not appear to be a number, attempts evaluating it as | ||
a parser functions expression. | a parser functions expression. | ||
خط ۴۹۱: | خط ۵۵۶: | ||
-- 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 | ||
خط ۵۱۸: | خط ۵۸۱: | ||
]] | ]] | ||
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) |