Skip to content

Commit

Permalink
Remove unnecessary restriction from bigint residuals
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed Dec 10, 2023
1 parent 7c850ba commit bf5cdc7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
14 changes: 14 additions & 0 deletions src/__tests__/monzo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
4 changes: 0 additions & 4 deletions src/monzo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit bf5cdc7

Please sign in to comment.