Created
November 5, 2014 01:25
-
-
Save ccwq/566491416b2d2e7d1400 to your computer and use it in GitHub Desktop.
javascript的mod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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