How do you get both the integer quotient and remainder of division in JavaScript? | A1Lab@FahadM to JavaScriptEnglish • 3 days ago

How do you get both the integer quotient and remainder of division in JavaScript?

message-square
0

To get both the integer quotient and remainder of division in JavaScript, compute the remainder with the % operator (remainder = dividend % divisor) and compute the quotient by dividing (dividend / divisor) and truncating toward zero with Math.trunc() (quotient = Math.trunc(dividend / divisor)), or use BigInt division (a / b) and % (a % b) for arbitrary-precision integers. Math.trunc() matches JavaScript’s % remainder sign convention (truncating toward zero rather than negative infinity like Math.floor()), and checking divisor === 0 avoids NaN or Infinity surprises.


1. Truncated Quotient and Remainder with Math.trunc() and %

Use Math.trunc(a / b) and a % b for standard IEEE-754 numbers when you want whole-number division truncated toward zero, matching how % behaves on positive and negative values:

// Compute truncated integer quotient and remainder for finite numbers
function divModTrunc(dividend, divisor) {
  if (typeof dividend !== "number" || typeof divisor !== "number") {
    throw new TypeError("Expected dividend and divisor to be numbers.");
  }

  if (divisor === 0) {
    throw new RangeError("Division by zero is not allowed.");
  }

  const quotient = Math.trunc(dividend / divisor);
  const remainder = dividend % divisor;

  return { quotient, remainder };
}

// Example usage:
console.log(divModTrunc(17, 5));   // { quotient: 3, remainder: 2 }
console.log(divModTrunc(-17, 5));  // { quotient: -3, remainder: -2 }
console.log(divModTrunc(17, -5));  // { quotient: -3, remainder: 2 }
  • Matches % sign behavior: Because Math.trunc() drops the fractional part toward zero, quotient * divisor + remainder equals dividend across positive and negative inputs.
  • Avoid Math.floor() for signed numbers if you use %: Math.floor(-17 / 5) yields -4 while -17 % 5 is -2, which would break quotient * divisor + remainder === dividend.

2. Floor Quotient for Non-Negative or Euclidean Division

When working with non-negative indices, pagination, or floor division toward negative infinity, Math.floor(a / b) gives the floor quotient while ((a % b) + b) % b produces a non-negative Euclidean remainder when b > 0:

// Compute floor quotient and non-negative remainder for positive divisor
function divModFloor(dividend, divisor) {
  if (divisor === 0) {
    throw new RangeError("Division by zero is not allowed.");
  }

  const quotient = Math.floor(dividend / divisor);
  const remainder = ((dividend % divisor) + Math.abs(divisor)) % Math.abs(divisor);

  return { quotient, remainder };
}

// Example usage:
console.log(divModFloor(17, 5));   // { quotient: 3, remainder: 2 }
console.log(divModFloor(-17, 5));  // { quotient: -4, remainder: 3 }
  • Safe for negative dividends in grid/wrap math: Wrapping negative indices with ((n % m) + m) % m avoids negative remainders in JavaScript.
  • Watch safe integer limits: Standard JavaScript number loses precision above Number.MAX_SAFE_INTEGER (9007199254740991).

3. Exact Large-Integer Division with BigInt

For integers larger than Number.MAX_SAFE_INTEGER, BigInt division (/) already truncates toward zero and % returns the exact remainder without floating-point rounding:

// Compute exact quotient and remainder for BigInt values
function divModBigInt(dividend, divisor) {
  if (typeof dividend !== "bigint" || typeof divisor !== "bigint") {
    throw new TypeError("Expected dividend and divisor to be BigInt values.");
  }

  if (divisor === 0n) {
    throw new RangeError("Division by zero is not allowed.");
  }

  return {
    quotient: dividend / divisor,
    remainder: dividend % divisor
  };
}

// Example usage:
const bigA = 1000000000000000000005n;
const bigB = 10n;
console.log(divModBigInt(bigA, bigB)); // { quotient: 100000000000000000000n, remainder: 5n }
  • No Math.trunc() needed on BigInt: Integer division on BigInt discards fractional remainders automatically.
  • Do not mix BigInt and Number: Convert inputs consistently to BigInt or Number before operating so JavaScript does not throw a TypeError.