Java中取模运算的结果正负性
2018-08-08 本文已影响21人
小林记
对整形int
public class Demo{
public static void main( String[] args ){
int a1 = 3, a2 = -3, b1 = 2, b2 = -2;
System.out.println( a1 % b1); // 1
System.out.println( a1 % b2); //1
System.out.println( a2 % b1); //-1
System.out.println( a2 % b2); //-1
}
}
基本结论:结果正负性取决于除数
对实形 double/float
double a1 = 3.0, a2 = -3.0, b1 = 2.0, b2 = -2.0;
System.out.println( a1 % b1); //1.0
System.out.println( a1 % b2); //1.0
System.out.println( a2 % b1); //-1.0
System.out.println( a2 % b2); //-1.0
基本结论:结果依旧取决于除数