//获取两个数的中间值(超大数值)
public class MidValue {
public static void main(String[] args) {
method1();//两数相加后除二
method2();//处理超大数值
}
private static void method1() {
int min = 10;
int max = 20;
int midValue = (max + min)/2;
System.out.println(midValue);
}
private static void method2() {
int min = Integer.MIN_VALUE-100;
int max = Integer.MIN_VALUE;
int midValue = (max+min)/2;
System.out.println(midValue);//最大值加上最小值的时候出现了溢出,精度损失,结果不对
int midValue2 =min+(max-min)/2;//先减法后加法,不会损失精度
System.out.println(midValue2);
}
}