fix: Dynamically calculate precision for scientific notation
Co-authored-by: aider (gemini/gemini-2.5-pro) <aider@aider.chat>
This commit is contained in:
@@ -286,7 +286,13 @@ function displayOutput(num) {
|
|||||||
}
|
}
|
||||||
var numStr = num.toString();
|
var numStr = num.toString();
|
||||||
if (typeof num === 'number' && (numStr.length > RESULT_MAX_LEN || (num !== 0 && Math.abs(num) < 1e-4))) {
|
if (typeof num === 'number' && (numStr.length > RESULT_MAX_LEN || (num !== 0 && Math.abs(num) < 1e-4))) {
|
||||||
let precision = RESULT_MAX_LEN - 5;
|
// dynamic precision based on exponent length
|
||||||
|
var exp = Math.floor(Math.log(Math.abs(num)) / Math.LN10);
|
||||||
|
var expLen = String(Math.abs(exp)).length;
|
||||||
|
var signLen = (num < 0) ? 1 : 0;
|
||||||
|
// Total length is sign + first-digit + dot + fraction + E + exp-sign + exp-digits
|
||||||
|
// So precision for fraction is MAX - (sign + first-digit + dot + E + exp-sign + exp-digits)
|
||||||
|
let precision = RESULT_MAX_LEN - (signLen + 1 + 1 + 1 + 1 + expLen);
|
||||||
if (precision < 0) precision = 0;
|
if (precision < 0) precision = 0;
|
||||||
num = toExponential(num, precision).replace("e", "E");
|
num = toExponential(num, precision).replace("e", "E");
|
||||||
} else {
|
} else {
|
||||||
|
|||||||
Reference in New Issue
Block a user