Quantcast
Channel: JavaScript % (modulo) gives a negative result for negative numbers - Stack Overflow
Browsing latest articles
Browse All 14 View Live

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

Fix negative modulo (reminder operator %)Simplified using ES6 Arrow function, and without dangerously extending the Number prototypeconst mod = (n, m) => (n % m + m) % m;console.log(mod(-90, 360));...

View Article



Answer by V. Rubinetti for JavaScript % (modulo) gives a negative result for...

For fun, here's a "wrap" function that works sorta like a modulo, except you can also specify the minimum value of the range (instead of it being 0):const wrap = (value = 0, min = 0, max = 10) =>...

View Article

Answer by maartenpaauw for JavaScript % (modulo) gives a negative result for...

There is a NPM package that will do the work for you. You can install it with the following command.npm install just-modulo --saveUsage copied from the READMEimport modulo from 'just-modulo';modulo(7,...

View Article

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

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){...

View Article

Answer by quasimodo for JavaScript % (modulo) gives a negative result for...

If x is an integer and n is a power of 2, you can use x & (n - 1) instead of x % n.> -13 & (64 - 1)51

View Article


Answer by JayCrossler for JavaScript % (modulo) gives a negative result for...

So it seems that if you're trying to mod around degrees (so that if you have -50 degrees - 200 degrees), you'd want to use something like:function modrad(m) { return ((((180+m) % 360) + 360) % 360)-180;}

View Article

Answer by zessx for JavaScript % (modulo) gives a negative result for...

This is not a bug, there's 3 functions to calculate modulo, you can use the one which fit your needs (I would recommend to use Euclidean function)Truncating the decimal part functionconsole.log( 41 % 7...

View Article

Answer by StuR for JavaScript % (modulo) gives a negative result for negative...

Using Number.prototype is SLOW, because each time you use the prototype method your number is wrapped in an Object. Instead of this:Number.prototype.mod = function(n) { return ((this % n) + n) %...

View Article


Answer by wisbucky for JavaScript % (modulo) gives a negative result for...

The accepted answer makes me a little nervous because it re-uses the % operator. What if Javascript changes the behavior in the future?Here is a workaround that does not re-use %:function mod(a, n) {...

View Article


Answer by Shanimal for JavaScript % (modulo) gives a negative result for...

A "mod" function to return a positive result.var mod = function (n, m) { var remain = n % m; return Math.floor(remain >= 0 ? remain : remain + m);};mod(5,22) // 5mod(25,22) // 3mod(-1,22) //...

View Article

Answer by Rob Sobers for JavaScript % (modulo) gives a negative result for...

The % operator in JavaScript is the remainder operator, not the modulo operator (the main difference being in how negative numbers are treated):-1 % 8 // -1, not 7

View Article

Answer by dheerosaur for JavaScript % (modulo) gives a negative result for...

Though it isn't behaving as you expected, it doesn't mean that JavaScript is not 'behaving'. It is a choice JavaScript made for its modulo calculation. Because, by definition either answer makes...

View Article

Answer by Enrique for JavaScript % (modulo) gives a negative result for...

Number.prototype.mod = function (n) {"use strict"; return ((this % n) + n) % n;};Taken from this article: The JavaScript Modulo Bug

View Article


JavaScript % (modulo) gives a negative result for negative numbers

According to Google Calculator(-13) % 64 is 51.According to Javascript (see this JSBin) it is -13.How do I fix this?

View Article
Browsing latest articles
Browse All 14 View Live




Latest Images

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