UsageEdit
Performs a calculation.
{{#invoke:Sandbox/Lemondoge|add|5|8}}
ExamplesEdit
{{#invoke:Sandbox/Lemondoge/Calc|add|3|6}}
returns 9
{{#invoke:Sandbox/Lemondoge/Calc|minus|6|3}}
returns 3
{{#invoke:Sandbox/Lemondoge/Calc|mod|3|15}}
returns 3
Error detectionEdit
The "times" function (but not the VERY similar "multiplication" function) will return a error like this: "You cannot multiply a number by zero.", if the second parameter is 0, unless the third parameter is set to 1. This works by using the code below.
if num2 == 0 and errordetect ~= 1 then
return RError("You cannot multiply a number by zero.")
else
return num1 * num2
end
Anti-not-a-number mechanismEdit
The "division" function (and it's similar "div" function) will normally return "-nan" if all 3 parameters are set to 0, but if the third parameter is 1, it will return "1" instead. This works with the code below:
if num1 == 0 and num2 == 0 and antinotanumber == 1 then
return 1
else
return num1 / num2
end
-------------This (test) module performs a calculation.-------------
local p = {}
local function RError(msg)
return mw.ustring.format('<b class="error">%s</b>', msg)
end
function p.add(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 + num2
end
function p.addition(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 + num2
end
function p.minus(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 - num2
end
function p.subtraction(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 - num2
end
function p.times(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
local errordetect = tonumber(frame.args[3])
if num2 == 0 and errordetect ~= 1 then
return RError("You cannot multiply a number by zero.")
else
return num1 * num2
end
end
function p.multiplication(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 * num2
end
function p.double(frame)
local num1 = tonumber(frame.args[1])
return num1 * 2
end
function p.div(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
local antinotanumber = tonumber(frame.args[3])
if num1 == 0 and num2 == 0 and antinotanumber == 1 then
return 1
else
return num1 / num2
end
end
function p.division(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
local antinotanumber = tonumber(frame.args[3])
if num1 == 0 and num2 == 0 and antinotanumber == 1 then
return 1
else
return num1 / num2
end
end
function p.mod(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 % num2
end
function p.modulo(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 % num2
end
function p.exp(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 ^ num2
end
function p.exponentiation(frame)
local num1 = tonumber(frame.args[1])
local num2 = tonumber(frame.args[2])
return num1 ^ num2
end
return p