Skip to content

Commit

Permalink
Implement ceiling modulo
Browse files Browse the repository at this point in the history
  • Loading branch information
frostburn committed May 22, 2024
1 parent 7c63127 commit c2c2230
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
30 changes: 29 additions & 1 deletion src/__tests__/fraction.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {describe, it, expect} from 'vitest';
import {Fraction, gcd, lcm, mmod} from '../fraction';
import {Fraction, gcd, lcm, mmod, modc} from '../fraction';

describe('gcd', () => {
it('can find the greatest common divisor of 12 and 15 (number)', () => {
Expand Down Expand Up @@ -78,6 +78,34 @@ describe('mmod', () => {
it('works with negative numbers (bigint)', () => {
expect(mmod(-5n, 3n)).toBe(1n);
});

it('produces NaN for 1 % 0', () => {
expect(mmod(1, 0)).toBeNaN();
});

it('throws for 1n % 0n', () => {
expect(() => mmod(1n, 0n)).toThrow();
});
});

describe('Ceiling modulo', () => {
it('works like clockwork', () => {
expect([...Array(13).keys()].map(i => modc(i, 12))).toEqual([
12, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12,
]);
});

it('works with negative numbers (bigint)', () => {
expect(modc(-5n, 3n)).toBe(1n);
});

it('produces 0 for 1 % 0', () => {
expect(modc(1, 0)).toBe(0);
});

it('produces 0n for 1n % 0n', () => {
expect(modc(1n, 0n)).toBe(0n);
});
});

describe('Fraction', () => {
Expand Down
16 changes: 16 additions & 0 deletions src/fraction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,22 @@ export function mmod(a: number | bigint, b: typeof a): typeof a {
return ((a % b) + b) % b;
}

/**
* Ceiling modulo.
* @param a The dividend.
* @param b The divisor.
* @returns The remainder of Euclidean division of a by b where b modc b === b.
*/
export function modc(a: number, b: number): number;
export function modc(a: bigint, b: bigint): bigint;
export function modc(a: number | bigint, b: typeof a): typeof a {
if (!b) {
return b;
}
// @ts-ignore
return ((a % b) + b) % b || b;
}

/**
*
* This class offers the possibility to calculate fractions.
Expand Down

0 comments on commit c2c2230

Please sign in to comment.