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

前端 未结 11 1469
死守一世寂寞
死守一世寂寞 2020-11-22 11:01

According to Google Calculator (-13) % 64 is 51.

According to Javascript (see this JSBin) it is -13.

How do I fix this

11条回答
  •  余生分开走
    2020-11-22 11:56

    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;
    }
    

提交回复
热议问题