Quantcast
Viewing latest article 1
Browse Latest Browse All 14

Answer by Roko C. Buljan for JavaScript % (modulo) gives a negative result for negative numbers

Fix negative modulo (reminder operator %)

Simplified using ES6 Arrow function, and without dangerously extending the Number prototype

const mod = (n, m) => (n % m + m) % m;console.log(mod(-90, 360));    //  270  (Instead of -90)

Viewing latest article 1
Browse Latest Browse All 14

Trending Articles