Module:Calculator widget: Difference between revisions

Content deleted Content added
m Bawolff moved page Module:Sandbox/Bawolff/calculator to Module:Calculator widget: Move to module space for potential use in Calculator article
m Reverted 1 edit by Nope2051 (talk) to last revision by Bawolff
 
(7 intermediate revisions by 2 users not shown)
Line 4:
 
local function getDisplay()
-- Normal calculators don't display "Infinity" when dividing by 0.
return 'iffinite(ifzero(displayY,x,y),ifzero(displayY,x,y),nan)'
end
 
Line 45 ⟶ 46:
function compute(op)
local res = {
{ 'x', 'ifzero(equalFlag,ifequal(percentFlag, 1, x*y/100, x),ifgreater(op,1,1,0))' },
{ 'y', 'switch(op,0,x+y,1,y-x,2,x*y,3,y/x,4,y)' },
--{ 'x', '0' },
Line 78 ⟶ 79:
function p.getWidget( frame )
local buttons = {
{ "MC", {{"mem", "0"}}, "Memory clear" },
{ "MR", {{"x", "mem"},{"displayY", 0}}, "Memory recall" },
{ "M−", {{"mem", "ifzero(displayY,mem-x,mem-y)"}}, "Subtract from memory" },
{ "M+", {{"mem", "ifzero(displayY,mem+x,mem+y)"}}, "Add to memory" },
{ "C", getClear(), "Clear" },
-- Its a bit unclear what the expected behaviour of pressing ± immediately after equal.
{ "±", {{'x', '0-x'}}, "Change sign" },
{ "%", {{'percentFlag', '1'}} },
-- A bit hacky, sqrt is the only unary operator. We want to make
Line 96 ⟶ 97:
{ "5", pressNumber(5) },
{ "6", pressNumber(6) },
{ "×", compute(2), "Multiply" },
{ "1", pressNumber(1) },
{ "2", pressNumber(2) },
{ "3", pressNumber(3) },
{ "−", compute(1), "Subtract" },
{ "0", pressNumber(0) },
{ ".", pressDecimal(), "Decimal point" },
{ "=", computeEqual() },
{ "+", compute(0), "Add" },
}
 
local calc = '<div class="calculatorwidget calculator-container" style="display:grid;grid-template-columns:repeat(4, 1fr);grid-gap:5px;min-width:256px;width:20ch;border:thin solid gray;padding: 5px; background: #eee">'
if frame.args['fallback'] == 'none' then
-- if no js, hide.
Line 119 ⟶ 120:
calc = calc .. 'display:none" class="calculatorgadget-enabled'
end
calc = calc .. '">{{Calculator codex text|default=0|id=ans|formula=' .. getDisplay() .. '|style=text-align:right;font-weight:bold;|readonly=1|NaN-text=Error|aria-label=Result}}</div>'
for i, v in ipairs(buttons) do
local calcType, calcWeight
Line 139 ⟶ 140:
formula = formula .. ';' .. v2[2]
end
-- Should the alt text also be a title attribute to make a tooltip?
calc = calc .. '{{calculator button|type='.. calcType ..'|weight='.. calcWeight .. '|for=' .. string.sub(forVar,2) .. '|formula=' .. string.sub(formula,2) .. '|contents='.. v[1] .. '}}'
local alt = v[3] ~= nil and '|alt=' .. v[3] or ''
calc = calc .. '{{calculator button|type='.. calcType ..'|weight='.. calcWeight .. alt .. '|for=' .. string.sub(forVar,2) .. '|formula=' .. string.sub(formula,2) .. '|contents='.. v[1] .. '}}'
end
calc = calc .. '{{calculator|type=hidden|id=x|default=0}}'
Line 149 ⟶ 152:
calc = calc .. '{{calculator|type=hidden|id=percentFlag|default=0}}'
calc = calc .. '{{calculator|type=hidden|id=equalFlag|default=0}}'
calc = calc .. '<templatestyles src="Module:Calculator widget/style.css"/>'
calc = calc .. '</div>'
if frame.args['fallback'] == 'none' then