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 52466cc
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 2 deletions.
20 changes: 20 additions & 0 deletions src/__tests__/monzo.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,26 @@ describe('Fraction to monzo converter', () => {
residual
).toBe(BigInt('123456789000000000000'));
});

it('leaves residual 0n for big int zero', () => {
const [monzo, residual] = toMonzoAndResidual(0n, 1);
expect(residual).toBe(0n);
expect(monzo).toHaveLength(1);
});

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: 2 additions & 2 deletions src/monzo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ function bigIntToMonzoAndResidual(
n: bigint,
numberOfComponents: number
): [Monzo, bigint] {
if (n < 1n) {
throw new Error('Cannot numbers smaller than one to monzo');
if (!n) {
return [Array(numberOfComponents).fill(0), 0n];
}

let probe = 1n;
Expand Down

0 comments on commit 52466cc

Please sign in to comment.