A robust modular arithmetic utility for JavaScript, handling both positive and negative integers with ease. Ensure consistent, non-negative results for your modulo operations. Perfect for mathematical computations, circular buffer implementations, and more.
npm install @hridel/modulo
import { mod } from '@hridel/modulo';
const exampleResult: number = mod(-1, 5);
console.log(`The result of -1 mod 5 is: ${exampleResult}`); // The result of -1 mod 5 is: 4
import { rem } from '@hridel/modulo';
// Example usage
const exampleResult: number = rem(-1, 5);
console.log(`The result of -1 rem 5 is: ${exampleResult}`); // The result of -1 rem 5 is: -1
import { div } from '@hridel/modulo';
// Example usage
const exampleResult: number = div(-1, 5);
console.log(`The result of -1 div 5 is: ${exampleResult}`); // The result of -1 div 5 is: -1
Because (-1 % 5)
in JavaScript returns -1
, which is not a valid result for a modulo operation.
This library ensures that the result of the mod
function is always a non-negative integer.
The rem
function returns the remainder of the division operation, which can be negative.
And the div
function returns the quotient rounded towards negative infinity, similar to Haskell's div
.