mirror of
https://github.com/stack-auth/stack.git
synced 2026-06-13 21:01:21 +08:00
19 lines
648 B
TypeScript
19 lines
648 B
TypeScript
/**
|
|
* Similar to the modulo operator, but always returns a positive number (even when the input is negative).
|
|
*/
|
|
export function remainder(n: number, d: number): number {
|
|
return ((n % d) + Math.abs(d)) % d;
|
|
}
|
|
import.meta.vitest?.test("remainder", ({ expect }) => {
|
|
expect(remainder(10, 3)).toBe(1);
|
|
expect(remainder(10, 5)).toBe(0);
|
|
expect(remainder(10, 7)).toBe(3);
|
|
// Test with negative numbers
|
|
expect(remainder(-10, 3)).toBe(2);
|
|
expect(remainder(-5, 2)).toBe(1);
|
|
expect(remainder(-7, 4)).toBe(1);
|
|
// Test with decimal numbers
|
|
expect(remainder(10.5, 3)).toBeCloseTo(1.5);
|
|
expect(remainder(-10.5, 3)).toBeCloseTo(1.5);
|
|
});
|