Math
绝对值
1. public static int abs(int a) {
return (a < 0) ? -a : a;
}
2. public static long abs(long a) {
return (a < 0) ? -a : a;
}
3. public static float abs(float a) {
return (a <= 0.0F) ? 0.0F - a : a;
}
4. public static double abs(double a) {
return (a <= 0.0D) ? 0.0D - a : a;
}
返回绝对值
加法
1. public static int addExact(int x, int y) {
int r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("integer overflow");
}
return r;
}
2.public static long addExact(long x, long y) {
long r = x + y;
// HD 2-12 Overflow iff both arguments have the opposite sign of the result
if (((x ^ r) & (y ^ r)) < 0) {
throw new ArithmeticException("long overflow");
}
return r;
}
返回其参数的总和,如果结果溢出则抛出异常
返回带有第二个浮点参数符号的第一个浮点参数
1.Math.copySign(double magnitude,double sign)
2.Math.copySign(float magnitude, float sign)
double a = Math.copySign(2.3, -10.0);
System.out.println(a);
结果为:-2.3
返回大于等于或小于等于的最小值或最大值
1.public static double ceil(double a) {
return StrictMath.ceil(a); // default impl. delegates to StrictMath
}
返回大于或等于参数且等于数学整数的最小值(最接近负无穷大) double
2.public static double floor(double a) {
return StrictMath.floor(a); // default impl. delegates to StrictMath
}
返回小于或等于参数且等于数学整数的最大值(最接近正无穷大) double
两数之间找最大值
1.public static int max/min(int a, int b) {
return (a >= b) ? a : b;
}
2.public static long max/min(long a, long b) {
return (a >= b) ? a : b;
}
3.public static float max/min(float a, float b) {
if (a != a)
return a; // a is NaN
if ((a == 0.0f) &&
(b == 0.0f) &&
(Float.floatToRawIntBits(a) == negativeZeroFloatBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a >= b) ? a : b;
}
4.public static double max/min(double a, double b) {
if (a != a)
return a; // a is NaN
if ((a == 0.0d) &&
(b == 0.0d) &&
(Double.doubleToRawLongBits(a) == negativeZeroDoubleBits)) {
// Raw conversion ok since NaN can't map to -0.0.
return b;
}
return (a >= b) ? a : b;
}
返回两个值中较大的
四舍五入
1.Math.round(double a/float a);
System.out.println(Math.round(-100.5)) -100 小数的负数的四舍五入与常规不同,需注意
返回与参数最接近的 long/int ,并将关系四舍五入为正无穷大
除法x/y
2.Math.floorDiv(int x, int y) 返回小于或等于代数商的最大值(最接近正无穷大) int
Math.floorDiv(long x, int y) 返回小于或等于代数商的最大值(最接近正无穷大) long
Math.floorDiv(long x, long y) 返回小于或等于代数商的最大值(最接近正无穷大) long
递减递增
3.public static int decrementExact(int a/long a) {
if (a == Integer.MIN_VALUE) {
throw new ArithmeticException("integer overflow");
}
return a - 1;
}
public static int incrementExact(int a) {
if (a == Integer.MAX_VALUE) {
throw new ArithmeticException("integer overflow");
}
return a + 1;
}
返回参数递减1,如果结果溢出 int/long则抛出异常
返回参数递加1,如果结果溢出 int/long则抛出异常
A*B+C
1..Math.fma(double a, double b, double c);
Math.fma(float a, float b, float c);
乘法
1.Math.multiplyExact(int x, int y);返回参数的乘积,如果结果溢出 int则抛出异常
Math.multiplyExact(long x, int y);返回参数的乘积,如果结果溢出 long则抛出异常
Math.multiplyExact(long x, long y);返回参数的乘积,如果结果溢出 long则抛出异常
相反数
1.Math.negateExact(int a);回参数的否定,如果结果溢出 int则抛出异常
Math.negateExact(long a); 回参数的否定,如果结果溢出 int则抛出异常
幂
1.Math.pow(double a, double b);返回第一个参数的值,该值是第二个参数的幂
减法x-y
1.Math.subtractExact(int x, int y)返回参数的差异,如果结果溢出 int则抛出异常
Math.subtractExact(long x, long y);返回参数的差异,如果结果溢出 long则抛出异常