Skip to content

Instantly share code, notes, and snippets.

@ccwq
Created November 5, 2014 01:25
Show Gist options
  • Save ccwq/566491416b2d2e7d1400 to your computer and use it in GitHub Desktop.
Save ccwq/566491416b2d2e7d1400 to your computer and use it in GitHub Desktop.
javascript的mod
javascript中取模运算的结果有时候是很让人伤心的
看下面的算式
3%4==3 //true
2%4==2 //true
1%4==1 //true
0%4==0 //true
-1%4==3 //false
.
.
.
实际结果
-1%4==-1//true
在大部分时间,这个结果不是你期望的。
于是就有了下面的方法
function modp(be_mod,mod){
((be_mod%mod)+mod)%mod;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment