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:
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user