Module:Calculator widget: Difference between revisions

Content deleted Content added
Round to reduce noticability of floating point error
Tag: Reverted
m Reverted 1 edit by Nope2051 (talk) to last revision by Bawolff
 
(3 intermediate revisions by 2 users not shown)
Line 5:
local function getDisplay()
-- Normal calculators don't display "Infinity" when dividing by 0.
return 'round(iffinite(ifzero(displayY,x,y),ifzero(displayY,x,y),nan),13)'
-- Round to 13 digits to reduce binary float issues
return 'round(iffinite(ifzero(displayY,x,y),ifzero(displayY,x,y),nan),13)'
end
 
Line 80 ⟶ 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 98 ⟶ 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" },
}
 
Line 121 ⟶ 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 141 ⟶ 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}}'