Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::open($save_path, $session_name) should either be compatible with SessionHandlerInterface::open(string $path, string $name): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 205
Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::close() should either be compatible with SessionHandlerInterface::close(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 220
Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::read($id) should either be compatible with SessionHandlerInterface::read(string $id): string|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 234
Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::write($id, $dataStr) should either be compatible with SessionHandlerInterface::write(string $id, string $data): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 262
Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::destroy($id) should either be compatible with SessionHandlerInterface::destroy(string $id): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 368
Deprecated: Return type of MediaWiki\Session\PHPSessionHandler::gc($maxlifetime) should either be compatible with SessionHandlerInterface::gc(int $max_lifetime): int|false, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/PHPSessionHandler.php on line 389
Deprecated: Return type of Wikimedia\Rdbms\IResultWrapper::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php on line 70
Deprecated: Return type of Wikimedia\Rdbms\IResultWrapper::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php on line 80
Deprecated: Return type of Wikimedia\Rdbms\IResultWrapper::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/IResultWrapper.php on line 75
Deprecated: Return type of Wikimedia\Rdbms\ResultWrapper::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php on line 117
Deprecated: Return type of Wikimedia\Rdbms\ResultWrapper::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php on line 90
Deprecated: Return type of MediaWiki\Session\Session::count() should either be compatible with Countable::count(): int, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 625
Deprecated: Return type of MediaWiki\Session\Session::current() should either be compatible with Iterator::current(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 631
Deprecated: Return type of MediaWiki\Session\Session::next() should either be compatible with Iterator::next(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 643
Deprecated: Return type of MediaWiki\Session\Session::key() should either be compatible with Iterator::key(): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 637
Deprecated: Return type of MediaWiki\Session\Session::valid() should either be compatible with Iterator::valid(): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 655
Deprecated: Return type of MediaWiki\Session\Session::rewind() should either be compatible with Iterator::rewind(): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 649
Deprecated: Return type of MediaWiki\Session\Session::offsetExists($offset) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 665
Deprecated: Return type of & MediaWiki\Session\Session::offsetGet($offset) should either be compatible with ArrayAccess::offsetGet(mixed $offset): mixed, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 678
Deprecated: Return type of MediaWiki\Session\Session::offsetSet($offset, $value) should either be compatible with ArrayAccess::offsetSet(mixed $offset, mixed $value): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 688
Deprecated: Return type of MediaWiki\Session\Session::offsetUnset($offset) should either be compatible with ArrayAccess::offsetUnset(mixed $offset): void, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice in /home/lfg/itchan.official.jp/public_html/wiki/includes/session/Session.php on line 693
Deprecated: Message implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /home/lfg/itchan.official.jp/public_html/wiki/includes/Message.php on line 159
Deprecated: RawMessage implements the Serializable interface, which is deprecated. Implement __serialize() and __unserialize() instead (or in addition, if support for old PHP versions is necessary) in /home/lfg/itchan.official.jp/public_html/wiki/includes/RawMessage.php on line 34
Deprecated: Creation of dynamic property RequestContext::$recursion is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/includes/context/RequestContext.php on line 326
Deprecated: Creation of dynamic property Parser::$mPreprocessorClass is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/includes/parser/Parser.php on line 279
Deprecated: Creation of dynamic property Parser::$extCite is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/extensions/Cite/includes/Cite.php on line 1296
Deprecated: Creation of dynamic property Parser::$scribunto_engine is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/extensions/Scribunto/includes/common/Hooks.php on line 81
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/includes/parser/Preprocessor_DOM.php on line 845
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/Feed.php on line 294
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/Feed.php on line 300
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/WebResponse.php on line 46
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/WebResponse.php on line 46
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/WebResponse.php on line 46
Warning: Cannot modify header information - headers already sent by (output started at /home/lfg/itchan.official.jp/public_html/wiki/includes/libs/rdbms/database/resultwrapper/ResultWrapper.php:24) in /home/lfg/itchan.official.jp/public_html/wiki/includes/WebResponse.php on line 46 http://itchan.official.jp/wiki/index.php?action=history&feed=atom&title=%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB%3AMathモジュール:Math - 版の履歴2026-03-25T18:10:16Zこのウィキのこのページに関する変更履歴MediaWiki 1.31.1 Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/includes/parser/Preprocessor_DOM.php on line 845
Deprecated: Creation of dynamic property PPDStack::$accum is deprecated in /home/lfg/itchan.official.jp/public_html/wiki/includes/parser/Preprocessor_DOM.php on line 845 http://itchan.official.jp/wiki/index.php?title=%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB:Math&diff=1071&oldid=prevいんふぉ™: 1版2014-03-20T16:07:16Z<p>1版</p>
<table class="diff diff-contentalign-left" data-mw="interface">
<tr class="diff-title" lang="ja">
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">← 古い版</td>
<td colspan="1" style="background-color: #fff; color: #222; text-align: center;">2014年3月20日 (木) 16:07時点における版</td>
</tr><tr><td colspan="2" class="diff-notice" lang="ja"><div class="mw-diff-empty">(相違点なし)</div>
</td></tr></table>いんふぉ™http://itchan.official.jp/wiki/index.php?title=%E3%83%A2%E3%82%B8%E3%83%A5%E3%83%BC%E3%83%AB:Math&diff=1070&oldid=prevLos688: en:Module:Math5 March 2013 at 03:27.より転記2013-03-20T09:52:16Z<p><a href="/wiki/index.php?title=En:Module:Math&action=edit&redlink=1" class="new" title="En:Module:Math (存在しないページ)">en:Module:Math</a>5 March 2013 at 03:27.より転記</p>
<p><b>新規ページ</b></p><div>--[[<br />
<br />
This module provides a number of basic mathematical operations.<br />
<br />
]]<br />
local z = {}<br />
<br />
-- Generate random number<br />
function z.random( frame )<br />
first = tonumber(frame.args[1]) -- if it doesn't exist it's NaN, if not a number it's nil<br />
second = tonumber(frame.args[2])<br />
<br />
if first then -- if NaN or nil, will skip down to final return<br />
if first <= second then -- could match if both nil, but already checked that first is a number in last line<br />
return math.random(first, second)<br />
end<br />
return math.random(first)<br />
end <br />
return math.random()<br />
end<br />
<br />
--[[<br />
order<br />
<br />
Determine order of magnitude of a number<br />
<br />
Usage:<br />
{{#invoke: Math | order | value }}<br />
]]<br />
function z.order(frame)<br />
local input_string = (frame.args[1] or frame.args.x or '0');<br />
local input_number;<br />
<br />
input_number = z._cleanNumber( frame, input_string );<br />
if input_number == nil then<br />
return '<strong class="error">Formatting error: Order of magnitude input appears non-numeric</strong>'<br />
else<br />
return z._order( input_number )<br />
end <br />
end<br />
function z._order(x)<br />
if x == 0 then return 0 end<br />
return math.floor(math.log10(math.abs(x)))<br />
end<br />
<br />
--[[<br />
precision<br />
<br />
Detemines the precision of a number using the string representation<br />
<br />
Usage:<br />
{{ #invoke: Math | precision | value }}<br />
]]<br />
function z.precision( frame )<br />
local input_string = (frame.args[1] or frame.args.x or '0');<br />
local trap_fraction = frame.args.check_fraction or false;<br />
local input_number;<br />
<br />
if type( trap_fraction ) == 'string' then<br />
trap_fraction = trap_fraction:lower();<br />
if trap_fraction == 'false' or trap_fraction == '0' or<br />
trap_fraction == 'no' or trap_fraction == '' then<br />
trap_fraction = false;<br />
else<br />
trap_fraction = true;<br />
end<br />
end<br />
<br />
if trap_fraction then<br />
local pos = string.find( input_string, '/', 1, true );<br />
if pos ~= nil then<br />
if string.find( input_string, '/', pos + 1, true ) == nil then<br />
local denominator = string.sub( input_string, pos+1, -1 );<br />
local denom_value = tonumber( denominator );<br />
if denom_value ~= nil then<br />
return math.log10(denom_value);<br />
end<br />
end <br />
end<br />
end <br />
<br />
input_number, input_string = z._cleanNumber( frame, input_string );<br />
if input_string == nil then<br />
return '<strong class="error">Formatting error: Precision input appears non-numeric</strong>'<br />
else<br />
return z._precision( input_string )<br />
end <br />
end<br />
function z._precision( x ) <br />
x = string.upper( x )<br />
<br />
local decimal = string.find( x, '.', 1, true )<br />
local exponent_pos = string.find( x, 'E', 1, true )<br />
local result = 0;<br />
<br />
if exponent_pos ~= nil then<br />
local exponent = string.sub( x, exponent_pos + 1 )<br />
x = string.sub( x, 1, exponent_pos - 1 )<br />
result = result - tonumber( exponent )<br />
end <br />
<br />
if decimal ~= nil then<br />
result = result + string.len( x ) - decimal<br />
return result<br />
end<br />
<br />
local pos = string.len( x );<br />
while x:byte(pos) == string.byte('0') do<br />
pos = pos - 1<br />
result = result - 1<br />
if pos <= 0 then<br />
return 0<br />
end<br />
end<br />
<br />
return result<br />
end<br />
<br />
--[[<br />
max<br />
<br />
Finds the maximum argument<br />
<br />
Usage:<br />
{{#invoke:Math| max | value1 | value2 | ... }}<br />
OR<br />
{{#invoke:Math| max }}<br />
<br />
When used with no arguments, it takes its input from the parent<br />
frame. Note, any values that do not evaluate to numbers are ignored.<br />
]]<br />
function z.max( frame )<br />
local args = frame.args;<br />
<br />
if args[1] == nil then<br />
local parent = frame:getParent();<br />
args = parent.args;<br />
end<br />
local max_value = nil;<br />
<br />
local i = 1;<br />
while args[i] ~= nil do<br />
local val = z._cleanNumber( frame, args[i] );<br />
if val ~= nil then<br />
if max_value == nil or val > max_value then<br />
max_value = val;<br />
end<br />
end <br />
i = i + 1;<br />
end<br />
<br />
return max_value<br />
end<br />
<br />
--[[<br />
min <br />
<br />
Finds the minimum argument<br />
<br />
Usage:<br />
{{#invoke:Math| min | value1 | value2 | ... }}<br />
OR<br />
{{#invoke:Math| min }}<br />
<br />
When used with no arguments, it takes its input from the parent<br />
frame. Note, any values that do not evaluate to numbers are ignored.<br />
]]<br />
function z.min( frame )<br />
local args = frame.args;<br />
<br />
if args[1] == nil then<br />
local parent = frame:getParent();<br />
args = parent.args;<br />
end<br />
local min_value = nil;<br />
<br />
local i = 1;<br />
while args[i] ~= nil do<br />
local val = z._cleanNumber( frame, args[i] );<br />
if val ~= nil then<br />
if min_value == nil or val < min_value then<br />
min_value = val;<br />
end<br />
end <br />
i = i + 1;<br />
end<br />
<br />
return min_value<br />
end<br />
<br />
--[[<br />
round<br />
<br />
Rounds a number to specified precision<br />
<br />
Usage:<br />
{{#invoke:Math | round | value | precision }}<br />
<br />
--]]<br />
function z.round(frame)<br />
local value, precision;<br />
<br />
value = z._cleanNumber( frame, frame.args[1] or frame.args.value or 0 );<br />
precision = z._cleanNumber( frame, frame.args[2] or frame.args.precision or 0 );<br />
<br />
if value == nil or precision == nil then<br />
return '<strong class="error">Formatting error: Round input appears non-numeric</strong>'<br />
else<br />
return z._round( value, precision );<br />
end <br />
end<br />
function z._round( value, precision )<br />
local rescale = math.pow( 10, precision );<br />
return math.floor( value * rescale + 0.5 ) / rescale;<br />
end<br />
<br />
--[[<br />
precision_format<br />
<br />
Rounds a number to the specified precision and formats according to rules <br />
originally used for {{template:Rnd}}. Output is a string.<br />
<br />
Usage:<br />
{{#invoke: Math | precision_format | number | precision }}<br />
]]<br />
function z.precision_format( frame )<br />
-- For access to Mediawiki built-in formatter.<br />
local lang = mw.getContentLanguage();<br />
<br />
local value_string, value, precision;<br />
value, value_string = z._cleanNumber( frame, frame.args[1] or 0 );<br />
precision = z._cleanNumber( frame, frame.args[2] or 0 );<br />
<br />
-- Check for non-numeric input<br />
if value == nil or precision == nil then<br />
return '<strong class="error">Formatting error: invalid input when rounding</strong>'<br />
end<br />
<br />
local current_precision = z._precision( value );<br />
<br />
local order = z._order( value );<br />
<br />
-- Due to round-off effects it is neccesary to limit the returned precision under<br />
-- some circumstances because the terminal digits will be inaccurately reported.<br />
if order + precision >= 14 then<br />
orig_precision = z._precision( value_string );<br />
if order + orig_precision >= 14 then<br />
precision = 13 - order; <br />
end <br />
end<br />
<br />
-- If rounding off, truncate extra digits<br />
if precision < current_precision then<br />
value = z._round( value, precision );<br />
current_precision = z._precision( value );<br />
end <br />
<br />
local formatted_num = lang:formatNum( math.abs(value) );<br />
local sign;<br />
<br />
-- Use proper unary minus sign rather than ASCII default<br />
if value < 0 then<br />
sign = '−';<br />
else<br />
sign = '';<br />
end <br />
<br />
-- Handle cases requiring scientific notation<br />
if string.find( formatted_num, 'E', 1, true ) ~= nil or math.abs(order) >= 9 then<br />
value = value * math.pow( 10, -order );<br />
current_precision = current_precision + order;<br />
precision = precision + order;<br />
formatted_num = lang:formatNum( math.abs(value) );<br />
else<br />
order = 0; <br />
end<br />
formatted_num = sign .. formatted_num;<br />
<br />
-- Pad with zeros, if needed <br />
if current_precision < precision then<br />
local padding;<br />
if current_precision <= 0 then<br />
if precision > 0 then<br />
local zero_sep = lang:formatNum( 1.1 );<br />
formatted_num = formatted_num .. zero_sep:sub(2,2);<br />
<br />
padding = precision;<br />
if padding > 20 then<br />
padding = 20;<br />
end<br />
<br />
formatted_num = formatted_num .. string.rep( '0', padding );<br />
end <br />
else <br />
padding = precision - current_precision<br />
if padding > 20 then<br />
padding = 20;<br />
end<br />
formatted_num = formatted_num .. string.rep( '0', padding );<br />
end<br />
end<br />
<br />
-- Add exponential notation, if necessary.<br />
if order ~= 0 then<br />
-- Use proper unary minus sign rather than ASCII default<br />
if order < 0 then<br />
order = '−' .. lang:formatNum( math.abs(order) );<br />
else<br />
order = lang:formatNum( order );<br />
end <br />
<br />
formatted_num = formatted_num .. '<span style="margin:0 .15em 0 .25em">×</span>10<sup>' .. order .. '</sup>'<br />
end<br />
<br />
return formatted_num;<br />
end<br />
<br />
--[[<br />
Helper function that interprets the input numerically. If the <br />
input does not appear to be a number, attempts evaluating it as<br />
a parser functions expression.<br />
]]<br />
<br />
function z._cleanNumber( frame, number_string )<br />
if number_string == nil or number_string:len() == 0 then<br />
return nil, nil;<br />
end <br />
<br />
-- Attempt basic conversion<br />
local number = tonumber( number_string )<br />
<br />
-- If failed, attempt to evaluate input as an expression<br />
if number == nil then <br />
local attempt = frame:preprocess( '{{#expr: ' .. number_string .. '}}' );<br />
attempt = tonumber( attempt );<br />
if attempt ~= nil then<br />
number = attempt;<br />
number_string = tostring( number );<br />
else<br />
number = nil;<br />
number_string = nil;<br />
end<br />
else<br />
-- String is valid but may contain padding, clean it.<br />
number_string = number_string:match( "^%s*(.-)%s*$" );<br />
end<br />
<br />
return number, number_string;<br />
end<br />
<br />
return z</div>Los688