Quantcast
Channel: JavaScript % (modulo) gives a negative result for negative numbers - Stack Overflow
Viewing all articles
Browse latest Browse all 14

Answer by bormat for JavaScript % (modulo) gives a negative result for negative numbers

$
0
0

I deal with négative a and negative n too

 //best perf, hard to read   function modul3(a,n){        r = a/n | 0 ;        if(a < 0){             r += n < 0 ? 1 : -1        }        return a - n * r     }    // shorter code    function modul(a,n){        return  a%n + (a < 0 && Math.abs(n));     }    //beetween perf and small code    function modul(a,n){        return a - n * Math[n > 0 ? 'floor' : 'ceil'](a/n);     }

Viewing all articles
Browse latest Browse all 14

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>