「モジュール:Math」の版間の差分

en:Module:Math oldid=915569429 より更新、エラーメッセージ変更
(1版 をインポートしました)
template>ネイ
(en:Module:Math oldid=915569429 より更新、エラーメッセージ変更)
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">Formatting error: %s</strong>', msg)
return mw.ustring.format('<strong class="error">書式エラー: %s</strong>', msg)
end
end


52行目: 52行目:
-- 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
117行目: 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('order of magnitude input appears non-numeric')
return err('order関数に渡された引数が数字ではありません')
else
else
return p._order(input_number)
return p._order(input_number)
end  
end
end
end


154行目: 154行目:
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('precision input appears non-numeric')
return err('precision関数に渡された引数が数字ではありません')
else
else
return p._precision(input_string)
return p._precision(input_string)
end  
end
end
end


180行目: 180行目:
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
238行目: 238行目:


function p._median(...)
function p._median(...)
local vals = makeArgArray(...)
local vals = makeArgArray(...)
local count = #vals
local count = #vals
table.sort(vals)
table.sort(vals)
 
if count == 0 then
if count == 0 then
return 0
return 0
end
end
 
if p._mod(count, 2) == 0 then
if p._mod(count, 2) == 0 then
return (vals[count/2] + vals[count/2+1])/2
return (vals[count/2] + vals[count/2+1])/2
254行目: 254行目:


--[[
--[[
min  
min


Finds the minimum argument
Finds the minimum argument
279行目: 279行目:


--[[
--[[
sum  
sum


Finds the sum
Finds the sum
305行目: 305行目:


--[[
--[[
average  
average


Finds the average
Finds the average
344行目: 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('round input appears non-numeric')
return err('round関数に渡された引数が数字ではありません')
else
else
return p._round(value, precision)
return p._round(value, precision)
end  
end
end
end


382行目: 382行目:
local y = p._cleanNumber(args[2])
local y = p._cleanNumber(args[2])
if not x then
if not x then
return err('first argument to mod appears non-numeric')
return err('mod関数に渡された第1引数が数字ではありません')
elseif not y then
elseif not y then
return err('second argument to mod appears non-numeric')
return err('mod関数に渡された第2引数が数字ではありません')
else
else
return p._mod(x, y)
return p._mod(x, y)
end  
end
end
end


431行目: 431行目:
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.


454行目: 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('invalid input when rounding')
return err('_precision_format関数に渡された引数が不正です')
end
end


463行目: 463行目:
-- 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
orig_precision = p._precision(value_string)
if order + p._precision(value_string) >= 14 then
if order + orig_precision >= 14 then
precision = 13 - order;
precision = 13 - order;      
end
end      
end
end


473行目: 472行目:
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))
483行目: 482行目:
else
else
sign = ''
sign = ''
end  
end


-- Handle cases requiring scientific notation
-- Handle cases requiring scientific notation
492行目: 491行目:
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
510行目: 509行目:


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
527行目: 526行目:
else
else
order = lang:formatNum(order)
order = lang:formatNum(order)
end  
end


formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>'
formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>'
536行目: 535行目:


--[[
--[[
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.
匿名利用者