From 212cbdb9b76d6fbfe70ebaf0899f3ec6849b69c6 Mon Sep 17 00:00:00 2001 From: Tanner Collin Date: Sat, 14 Mar 2026 13:55:25 -0600 Subject: [PATCH] fix: Implement toExponential polyfill for broader compatibility Co-authored-by: aider (gemini/gemini-2.5-pro) --- calculator/calculator.app.js | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/calculator/calculator.app.js b/calculator/calculator.app.js index 77bf8e7..acef778 100644 --- a/calculator/calculator.app.js +++ b/calculator/calculator.app.js @@ -206,6 +206,29 @@ function subtract(x, y) { return sum(x, -y); } +function toExponential(num, precision) { + if (num === 0) { + let z = "0"; + if (precision > 0) z += "." + "0".repeat(precision); + return z + "e+0"; + } + var sign = ""; + if (num < 0) { + sign = "-"; + num = -num; + } + var exp = Math.floor(Math.log10(num)); + var mantissa = num / Math.pow(10, exp); + + var mantissaFixed = mantissa.toFixed(precision); + if (parseFloat(mantissaFixed) >= 10) { + mantissaFixed = (mantissa/10).toFixed(precision); + exp++; + } + + return sign + mantissaFixed + "e" + (exp >= 0 ? "+" : "") + exp; +} + function fixFloat(n) { if (Math.abs(n) < 1e-10) return 0; return n; @@ -265,7 +288,7 @@ function displayOutput(num) { if (typeof num === 'number' && (numStr.length > RESULT_MAX_LEN || (num !== 0 && Math.abs(num) < 1e-4))) { let precision = RESULT_MAX_LEN - 8; if (precision < 0) precision = 0; - num = num.toExponential(precision).replace("e", "E"); + num = toExponential(num, precision).replace("e", "E"); } else { num = numStr; }