Editing Module:Math

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 49: Line 49:
 
end
 
end
  
local function fold(func, ...)
+
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.
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
Line 63: Line 63:
 
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
  
Line 120: Line 112:
 
else
 
else
 
return p._order(input_number)
 
return p._order(input_number)
end
+
end  
 
end
 
end
  
Line 154: Line 146:
 
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);
Line 163: Line 155:
 
else
 
else
 
return p._precision(input_string)
 
return p._precision(input_string)
end
+
end  
 
end
 
end
  
Line 180: Line 172:
 
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
Line 198: Line 190:
 
return result
 
return result
 
end
 
end
 
  
 
--[[
 
--[[
Line 216: Line 207:
  
 
function p._max(...)
 
function p._max(...)
local max_value = binary_fold((function(a, b) return a > b end), ...)
+
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
Line 223: Line 221:
  
 
--[[
 
--[[
median
+
min  
 
 
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
Line 272: Line 239:
  
 
function p._min(...)
 
function p._min(...)
local min_value = binary_fold((function(a, b) return a < b end), ...)
+
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
Line 279: Line 253:
  
 
--[[
 
--[[
sum
+
average  
 
 
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
Line 322: Line 270:
  
 
function p._average(...)
 
function p._average(...)
local sum, count = fold((function(a, b) return a + b end), ...)
+
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
Line 347: Line 298:
 
else
 
else
 
return p._round(value, precision)
 
return p._round(value, precision)
end
+
end  
 
end
 
end
  
Line 353: Line 304:
 
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
  
Line 387: Line 325:
 
else
 
else
 
return p._mod(x, y)
 
return p._mod(x, y)
end
+
end  
 
end
 
end
  
Line 424: Line 362:
 
return oldr
 
return oldr
 
end
 
end
local result, count = fold(findGcd, ...)
+
local result, count = applyFuncToArgs(findGcd, ...)
 
return result
 
return result
 
end
 
end
Line 431: Line 369:
 
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.
  
Line 463: Line 401:
 
-- 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
+
orig_precision = p._precision(value_string)
precision = 13 - order;
+
if order + orig_precision >= 14 then
end
+
precision = 13 - order;      
 +
end      
 
end
 
end
  
Line 472: Line 411:
 
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))
Line 482: Line 421:
 
else
 
else
 
sign = ''
 
sign = ''
end
+
end  
  
 
-- Handle cases requiring scientific notation
 
-- Handle cases requiring scientific notation
Line 491: Line 430:
 
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
Line 509: Line 448:
  
 
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
Line 526: Line 465:
 
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>'
Line 535: Line 474:
  
 
--[[
 
--[[
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.

Please note that all contributions to All About Ayrshire may be edited, altered, or removed by other contributors. If you do not want your writing to be edited mercilessly, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource (see All About Ayrshire:Copyrights for details). Do not submit copyrighted work without permission!

Cancel Editing help (opens in new window)

Template used on this page: