From 84d68e8d9ca3c6300cddb00c37d7211bac565877 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Tue, 12 Dec 2023 08:03:31 +0200 Subject: [PATCH] Make defloat error reporting more accurate --- src/__tests__/fraction.spec.ts | 6 ++++++ src/fraction.ts | 3 +++ 2 files changed, 9 insertions(+) diff --git a/src/__tests__/fraction.spec.ts b/src/__tests__/fraction.spec.ts index c6853c5..7ea9203 100644 --- a/src/__tests__/fraction.spec.ts +++ b/src/__tests__/fraction.spec.ts @@ -221,4 +221,10 @@ describe('Fraction', () => { const fraction = new Fraction(13, 11); expect(fraction.divisible('conquer')).toBe(false); }); + + it('gives the correct error for too large components', () => { + expect(() => new Fraction(1.1231233477899796e16, 1)).toThrowError( + 'Numerator above safe limit' + ); + }); }); diff --git a/src/fraction.ts b/src/fraction.ts index 55dc627..69488d0 100644 --- a/src/fraction.ts +++ b/src/fraction.ts @@ -247,6 +247,9 @@ export class Fraction { } x = 1 / (x - coef); } + if (!coefs.length) { + throw new Error('Numerator above safe limit'); + } let n = coefs.pop()!; let d = 1; while (coefs.length) {