From 8c7c4889679ea6905e7ef1245515352c26dc520b Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 14 Mar 2026 14:34:24 -0600 Subject: [PATCH] refactor: Simplify math operations, remove custom precision functions Co-authored-by: aider (gemini/gemini-2.5-pro) --- calculator/calculator.app.js | 70 ++++++------------------------------ 1 file changed, 11 insertions(+), 59 deletions(-) diff --git a/calculator/calculator.app.js b/calculator/calculator.app.js index 286df98..32bd454 100644 --- a/calculator/calculator.app.js +++ b/calculator/calculator.app.js @@ -165,57 +165,6 @@ function drawSpecials() { drawKeys(); } -function getIntWithPrecision(x) { - var xStr = x.toString(); - if (xStr.indexOf('e') > -1) { - var parts = xStr.split('e'); - var mantissa = parts[0]; - var exponent = parseInt(parts[1], 10); - var mantissaRadix = mantissa.indexOf('.'); - var mantissaPrecision = mantissaRadix === -1 ? 0 : mantissa.length - mantissaRadix - 1; - return { - num: parseInt(mantissa.replace('.', ''), 10), - p: mantissaPrecision - exponent - }; - } - var xRadix = xStr.indexOf('.'); - var xPrecision = xRadix === -1 ? 0 : xStr.length - xRadix - 1; - return { - num: parseInt(xStr.replace('.', ''), 10), - p: xPrecision - }; -} - -function multiply(x, y) { - var xNum = getIntWithPrecision(x); - var yNum = getIntWithPrecision(y); - return xNum.num * yNum.num / Math.pow(10, xNum.p + yNum.p); -} - -function divide(x, y) { - var xNum = getIntWithPrecision(x); - var yNum = getIntWithPrecision(y); - return xNum.num / yNum.num / Math.pow(10, xNum.p - yNum.p); -} - -function sum(x, y) { - let xNum = getIntWithPrecision(x); - let yNum = getIntWithPrecision(y); - - let diffPrecision = Math.abs(xNum.p - yNum.p); - if (diffPrecision > 0) { - if (xNum.p > yNum.p) { - yNum.num = yNum.num * Math.pow(10, diffPrecision); - } else { - xNum.num = xNum.num * Math.pow(10, diffPrecision); - } - } - return (xNum.num + yNum.num) / Math.pow(10, Math.max(xNum.p, yNum.p)); -} - -function subtract(x, y) { - return sum(x, -y); -} function toExponential(num, precision) { if (num === 0) { @@ -246,15 +195,17 @@ function fixFloat(n) { } function doMath(x, y, operator) { + x = parseFloat(x); + y = parseFloat(y); switch (operator) { case '/': - return divide(x, y); + return x / y; case '*': - return multiply(x, y); + return x * y; case '+': - return sum(x, y); + return x + y; case '-': - return subtract(x, y); + return x - y; } } @@ -407,10 +358,11 @@ function buttonPress(val) { break; case 's': if (results != null) { - results = multiply(results, results); + results = results * results; displayOutput(results); } else if (currNumber != null) { - currNumber = multiply(currNumber, currNumber); + var num = parseFloat(currNumber); + currNumber = num * num; displayOutput(currNumber); } hasPressedNumber = false; @@ -520,10 +472,10 @@ function buttonPress(val) { break; case 'i': if (results != null) { - results = divide(1, results); + results = 1 / results; displayOutput(results); } else if (currNumber != null) { - currNumber = divide(1, currNumber); + currNumber = 1 / parseFloat(currNumber); displayOutput(currNumber); } hasPressedNumber = false;