From bf5cdc7294263a783d5ef9746644d149a2c61053 Mon Sep 17 00:00:00 2001 From: Lumi Pakkanen Date: Sun, 10 Dec 2023 21:44:06 +0200 Subject: [PATCH] Remove unnecessary restriction from bigint residuals --- src/__tests__/monzo.spec.ts | 14 ++++++++++++++ src/monzo.ts | 4 ---- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/__tests__/monzo.spec.ts b/src/__tests__/monzo.spec.ts index afbe547..ab9fc3b 100644 --- a/src/__tests__/monzo.spec.ts +++ b/src/__tests__/monzo.spec.ts @@ -119,6 +119,20 @@ describe('Fraction to monzo converter', () => { residual ).toBe(BigInt('123456789000000000000')); }); + + it('leaves residual 0n for big int zero (no vector part)', () => { + const [monzo, residual] = toMonzoAndResidual(0n, 0); + expect(residual).toBe(0n); + expect(monzo).toHaveLength(0); + }); + + it('leaves negative residual for big integers', () => { + const [monzo, residual] = toMonzoAndResidual(-10n, 2); + expect(residual).toBe(-5n); + expect(monzo).toHaveLength(2); + expect(monzo[0]).toBe(1); + expect(monzo[1]).toBe(0); + }); }); describe('Monzo to fraction converter', () => { diff --git a/src/monzo.ts b/src/monzo.ts index 23dda33..dceb16a 100644 --- a/src/monzo.ts +++ b/src/monzo.ts @@ -288,10 +288,6 @@ function bigIntToMonzoAndResidual( n: bigint, numberOfComponents: number ): [Monzo, bigint] { - if (n < 1n) { - throw new Error('Cannot numbers smaller than one to monzo'); - } - let probe = 1n; const result = Array(numberOfComponents).fill(-1);