💾modulo(a, b)
modulo
Last updated
Was this helpful?
modulo
Last updated
Was this helpful?
Was this helpful?
JS ⟩ operator ⟩ arithmetic ⟩ modulo
modulo(a, b) returns the modulo of a / b. (b decides the sign of the modulo)
(👉 see remainder vs. modulo vs. mod for more info)
replit: modulo(a,b) vs. mod(a,b)
// ⭐️ modulo: r = a - b*q
// ------------------------
// • r has same sign as b.
// (that is, `b` decides the "sign" of the result)
function modulo(a, b) {
return ((a % b) + b) % b
}
💈範例:
// remainder (a % b)
//┌─── `a` decides the "sign" ⭐️
//│
5 % 3, // 2
5 % -3, // 2
-5 % 3,