Difference between revisions of "Module:Sandbox/IJReid/Mathematics"
Jump to navigation
Jump to search
blackwiki>IJReid (math module testing) |
blackwiki>IJReid (allow for multiple-argument addition and multiplication) |
||
| Line 1: | Line 1: | ||
| − | -- | + | --Module to calculate basic math values |
| − | |||
| − | |||
mathematics = {} | mathematics = {} | ||
| − | + | mathematics.addition = function(input) --Addition function | |
| − | mathematics.addition = function(input) | + | local sum --Define variable for use in loop |
| − | local sum | + | for arg, i in pairs(input.args) do --Loop to run through all arguments |
| − | return sum | + | sum = sum + arg --Add argument value to sum |
| + | end | ||
| + | return sum --Output the sum to the module | ||
end | end | ||
| − | + | mathematics.subtraction = function(input) --Subtraction function | |
| − | mathematics.subtraction = function(input) | + | local dif = input.args[1] - input.args[2] --Subtract second argument from first |
| − | local | + | return dif --Outpud difference to module |
| − | return difference | ||
end | end | ||
| − | + | mathematics.multiplication = function(input) --Multiplication function | |
| − | mathematics.multiplication = function(input) | + | local product = 1 --Set product as 1 (so multiplication of it works) |
| − | local product = input.args | + | local pi = 3.141592653589793 --Set pi as 15 digits of precision |
| − | return product | + | for arg, i in pairs(input.args) do --Allow multiple arguments |
| + | if (arg == "pi" or arg == "π" or arg == 3.14) then --Set pi to π argument | ||
| + | arg = pi | ||
| + | else | ||
| + | product = product * arg --Multiply arguments together | ||
| + | end | ||
| + | end | ||
| + | return product --Output product to module | ||
end | end | ||
| − | + | mathematics.division = function(input) --Division function | |
| − | mathematics.division = function(input) | + | local quotient = input.args[1] / input.args[2] --Divide arg1 by arg2 |
| − | local quotient = input.args[1] / input.args[2] | + | return quotient --Output quotient to module |
| − | return quotient | ||
end | end | ||
| − | -- | + | return mathematics --Output module to place of use |
| − | |||
Revision as of 20:18, 22 October 2017
| This module is rated as alpha. It is ready for third-party input, and may be used on a few pages to see if problems arise, but should be watched. Suggestions for new features or changes in their input and output mechanisms are welcome. |
Usage
{{#invoke:Sandbox/IJReid|function_name}}
- Functions
- Multiplication -
{{#invoke:Sandbox/IJReid|multiplication|case_1|case_2}} - Division -
{{#invoke:Sandbox/IJReid|division|case_1|case_2}} - Subtraction -
{{#invoke:Sandbox/IJReid|subtraction|case_1|case_2}} - Addition -
{{#invoke:Sandbox/IJReid|addition|case_1|case_2}}
- Calculations for archival templates
{{#invoke:Sandbox/IJReid/Mathematics|subtraction|{{#invoke:Sandbox/IJReid/Mathematics|multiplication|{{Age in days|01 January {{safesubst:CURRENTYEAR}}}}|24}}|{{#invoke:Sandbox/IJReid/Mathematics|subtraction|24|{{safesubst:CURRENTHOUR}}}}}}- -5
- Lua error at line 7: attempt to perform arithmetic on local 'sum' (a nil value).
--Module to calculate basic math values
mathematics = {}
mathematics.addition = function(input) --Addition function
local sum --Define variable for use in loop
for arg, i in pairs(input.args) do --Loop to run through all arguments
sum = sum + arg --Add argument value to sum
end
return sum --Output the sum to the module
end
mathematics.subtraction = function(input) --Subtraction function
local dif = input.args[1] - input.args[2] --Subtract second argument from first
return dif --Outpud difference to module
end
mathematics.multiplication = function(input) --Multiplication function
local product = 1 --Set product as 1 (so multiplication of it works)
local pi = 3.141592653589793 --Set pi as 15 digits of precision
for arg, i in pairs(input.args) do --Allow multiple arguments
if (arg == "pi" or arg == "π" or arg == 3.14) then --Set pi to π argument
arg = pi
else
product = product * arg --Multiply arguments together
end
end
return product --Output product to module
end
mathematics.division = function(input) --Division function
local quotient = input.args[1] / input.args[2] --Divide arg1 by arg2
return quotient --Output quotient to module
end
return mathematics --Output module to place of use