1,507
个编辑
wp>Xiplus 小 (已更改“模块:Math”的保护等级:高风险模板([编辑=仅管理员](无限期)[移动=仅管理员](无限期))) |
小 (导入1个版本) |
||
| (未显示另一用户的1个中间版本) | |||
| 第1行: | 第1行: | ||
--[[ | --[[ | ||
引自维基百科(enwiki:Module:Math,oldid=613638641) | |||
This module provides a number of basic mathematical operations. | This module provides a number of basic mathematical operations. | ||
| 第49行: | 第50行: | ||
end | end | ||
local function | local function applyFuncToArgs(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. | ||
| 第63行: | 第64行: | ||
end | end | ||
return ret, count | return ret, count | ||
end | end | ||
| 第198行: | 第191行: | ||
return result | return result | ||
end | end | ||
--[[ | --[[ | ||
| 第216行: | 第208行: | ||
function p._max(...) | function p._max(...) | ||
local | local function maxOfTwo(a, b) | ||
if a > b then | |||
return a | |||
else | |||
return b | |||
end | |||
end | |||
local max_value = applyFuncToArgs(maxOfTwo, ...) | |||
if max_value then | if max_value then | ||
return max_value | return max_value | ||
| 第241行: | 第240行: | ||
function p._min(...) | function p._min(...) | ||
local | local function minOfTwo(a, b) | ||
if a < b then | |||
return a | |||
else | |||
return b | |||
end | |||
end | |||
local min_value = applyFuncToArgs(minOfTwo, ...) | |||
if min_value then | if min_value then | ||
return min_value | return min_value | ||
| 第265行: | 第271行: | ||
function p._average(...) | function p._average(...) | ||
local | local function getSum(a, b) | ||
return a + b | |||
end | |||
local sum, count = applyFuncToArgs(getSum, ...) | |||
if not sum then | if not sum then | ||
return 0 | return 0 | ||
| 第354行: | 第363行: | ||
return oldr | return oldr | ||
end | end | ||
local result, count = | local result, count = applyFuncToArgs(findGcd, ...) | ||
return result | return result | ||
end | end | ||