💾mod(a, b)
modulo
Last updated
Was this helpful?
modulo
Last updated
Was this helpful?
Was this helpful?
JS ⟩ operator ⟩ arithmetic ⟩ mod
mod(a, b) returns the nonnegative remainder of a / b.
replit: modulo(a,b) vs. mod(a,b)
// ⭐️ mod: r = a - b*q
// ------------------------
// • 0 <= r < |b| (remainder in math)
function mod(a, b) {
b = Math.abs(b);
return ((a % b) + b) % b;
}
💈範例:
// remainder (a % b)
//┌─── `a` decides the "sign" ⭐️
//│
5 % 3, // 2
5 % -3, // 2
-5 % 3,