refactor: Simplify math operations, remove custom precision functions

Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
2026-03-14 14:34:24 -06:00
committed by Tanner
parent 3507d8938e
commit 8c7c488967
+11 -59
View File
@@ -165,57 +165,6 @@ function drawSpecials() {
drawKeys(); 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) { function toExponential(num, precision) {
if (num === 0) { if (num === 0) {
@@ -246,15 +195,17 @@ function fixFloat(n) {
} }
function doMath(x, y, operator) { function doMath(x, y, operator) {
x = parseFloat(x);
y = parseFloat(y);
switch (operator) { switch (operator) {
case '/': case '/':
return divide(x, y); return x / y;
case '*': case '*':
return multiply(x, y); return x * y;
case '+': case '+':
return sum(x, y); return x + y;
case '-': case '-':
return subtract(x, y); return x - y;
} }
} }
@@ -407,10 +358,11 @@ function buttonPress(val) {
break; break;
case 's': case 's':
if (results != null) { if (results != null) {
results = multiply(results, results); results = results * results;
displayOutput(results); displayOutput(results);
} else if (currNumber != null) { } else if (currNumber != null) {
currNumber = multiply(currNumber, currNumber); var num = parseFloat(currNumber);
currNumber = num * num;
displayOutput(currNumber); displayOutput(currNumber);
} }
hasPressedNumber = false; hasPressedNumber = false;
@@ -520,10 +472,10 @@ function buttonPress(val) {
break; break;
case 'i': case 'i':
if (results != null) { if (results != null) {
results = divide(1, results); results = 1 / results;
displayOutput(results); displayOutput(results);
} else if (currNumber != null) { } else if (currNumber != null) {
currNumber = divide(1, currNumber); currNumber = 1 / parseFloat(currNumber);
displayOutput(currNumber); displayOutput(currNumber);
} }
hasPressedNumber = false; hasPressedNumber = false;