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) {