diff --git a/src/helpers/helpers.math.ts b/src/helpers/helpers.math.ts index 0fd2a95132c..de784e5f1a8 100644 --- a/src/helpers/helpers.math.ts +++ b/src/helpers/helpers.math.ts @@ -116,6 +116,9 @@ export function _decimalPlaces(x: number) { while (Math.round(x * e) / e !== x) { e *= 10; p++; + if (!isFinite(e)) { + return; + } } return p; } diff --git a/test/specs/helpers.math.tests.js b/test/specs/helpers.math.tests.js index 938742959da..4a7fbf3e932 100644 --- a/test/specs/helpers.math.tests.js +++ b/test/specs/helpers.math.tests.js @@ -36,6 +36,9 @@ describe('Chart.helpers.math', function() { expect(decimalPlaces(undefined)).toBe(undefined); expect(decimalPlaces(12345678.1234)).toBe(4); expect(decimalPlaces(1234567890.1234567)).toBe(7); + // Numbers so small that the multiplier overflows to Infinity — must not loop forever + expect(decimalPlaces(1e-305)).toBe(undefined); + expect(decimalPlaces(Number.MIN_VALUE)).toBe(undefined); }); it('should get an angle from a point', function() {