diff --git a/CHANGELOG.md b/CHANGELOG.md index 59f659ff1..7c7f2a6ed 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -68,6 +68,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), - Fixed the `ADDRESS` function adding the sheet separator (`!`) when the `sheetName` argument is an empty argument slot (e.g. `=ADDRESS(2,3,1,FALSE(),)` now returns `R2C3` instead of `!R2C3`), while an explicit `""` still adds it. [#1641](https://github.com/handsontable/hyperformula/issues/1641) - Fixed `IMSUM`, `IMPRODUCT` and `TEXTJOIN` throwing an uncaught `RangeError` for very large ranges, such as `=IMSUM(A1:D40000)`, which stopped the recalculation; they now return a result or an error value in the cell. [#1901](https://github.com/handsontable/hyperformula/pull/1901) - Fixed `AVEDEV`, `DEVSQ`, `GCD`, `GEOMEAN`, `HARMEAN`, `LCM`, `MEDIAN`, `NPV`, `SKEW` and `SKEW.P` stopping the engine from building on a range of about 125,000 cells or more. [#1800](https://github.com/handsontable/hyperformula/pull/1800) +- Fixed cell values smaller than about `1e-298` being returned as `NaN` instead of the rounded number when the `smartRounding` option is on, which is the default. [#1837](https://github.com/handsontable/hyperformula/pull/1837) ## [3.5.0] - 2026-10-09 diff --git a/hyperformula/src/Exporter.ts b/hyperformula/src/Exporter.ts index 4ffdaf3a6..1b782cfa2 100644 --- a/hyperformula/src/Exporter.ts +++ b/hyperformula/src/Exporter.ts @@ -52,6 +52,9 @@ export class ExportedNamedExpressionChange { } } +/** The largest number of significant digits Number.prototype.toPrecision accepts. */ +const MAX_TO_PRECISION_DIGITS = 100 + export class Exporter implements ChangeExporter { constructor( private readonly config: Config, @@ -132,6 +135,10 @@ export class Exporter implements ChangeExporter { } const magnitudeMultiplierExponent = Math.floor(Math.log10(Math.abs(value))) const placesMultiplier = Math.pow(10, this.config.precisionRounding - magnitudeMultiplierExponent) + if (placesMultiplier === Infinity) { + // For tiny values the multiplier overflows and the formula below would return Infinity / Infinity = NaN. + return Number(value.toPrecision(Math.min(this.config.precisionRounding + 1, MAX_TO_PRECISION_DIGITS))) + } if (value < 0) { return -Math.round(-value * placesMultiplier) / placesMultiplier } else {