https://wiki.casperdns.com/index.php?title=Module:Math&feed=atom&action=history
Module:Math - Revision history
2024-03-29T02:28:31Z
Revision history for this page on the wiki
MediaWiki 1.35.0
https://wiki.casperdns.com/index.php?title=Module:Math&diff=12282&oldid=prev
D1cd5b71-6209-4595-9bf0-771bf689ce00: 1 revision imported
2018-12-09T20:08:43Z
<p>1 revision imported</p>
<table class="diff diff-contentalign-left diff-editfont-monospace" data-mw="interface">
<col class="diff-marker" />
<col class="diff-content" />
<col class="diff-marker" />
<col class="diff-content" />
<tr class="diff-title" lang="en">
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">← Older revision</td>
<td colspan="2" style="background-color: #fff; color: #202122; text-align: center;">Revision as of 20:08, 9 December 2018</td>
</tr>
<!-- diff cache key wiki_casper-rnd2ja:diff::1.12:old-12281:rev-12282 -->
</table>
D1cd5b71-6209-4595-9bf0-771bf689ce00
https://wiki.casperdns.com/index.php?title=Module:Math&diff=12281&oldid=prev
en>Primefac: fix calculation
2018-09-30T23:27:34Z
<p>fix calculation</p>
<a href="https://wiki.casperdns.com/index.php?title=Module:Math&diff=12281&oldid=1818">Show changes</a>
en>Primefac
https://wiki.casperdns.com/index.php?title=Module:Math&diff=1818&oldid=prev
D1cd5b71-6209-4595-9bf0-771bf689ce00: 1 revision
2013-06-28T13:07:41Z
<p>1 revision</p>
<p><b>New page</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 />
average <br />
<br />
Finds the average<br />
<br />
Usage:<br />
{{#invoke:Math| average | value1 | value2 | ... }}<br />
OR<br />
{{#invoke:Math| average }}<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.average( frame )<br />
local args = frame.args;<br />
if args[1] == nil then<br />
local parent = frame:getParent();<br />
args = parent.args;<br />
end<br />
local sum = 0;<br />
local count = 0;<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 />
sum = sum + val<br />
count = count + 1<br />
end <br />
i = i + 1;<br />
end<br />
<br />
return (count == 0 and 0 or sum/count)<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>
D1cd5b71-6209-4595-9bf0-771bf689ce00