public static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
public static native double pow(double a, double b);
字段
- 注意:Math 类中对 PI 赋的值为【3.1415_92653_58979_32384_6,精度为21位】,实际上double的精度只能精确到【3.1415_92653_58979_3,精度为15~16位】
- 之所以这么设计,可能是考虑到以后可能会扩展double类型的精度
- 注意:Math 类中对 E 赋的值为【2.7182_81828_45904_52354,精度为20位】,实际上double的精度只能精确到【2.7182_81828_45904_5,精度为15~16位】
- 之所以这么设计,可能是考虑到以后可能会扩展double类型的精度
System.out.println(Math.PI+" "+Math.E);//3.141592653589793 2.718281828459045
近似值:round、ceil、floor、rint
static double floor(double a) 返回最大的(最接近正无穷大)double 值,该值小于等于参数,并等于某个整数。
static double rint(double a) 返回最接近参数并等于某一整数的 double 值。 如果两个同为整数的 double 值都同样接近,那么结果取偶数。
static long round(double a) 返回最接近参数的 long。
static int round(float a) 返回最接近参数的 int。
- round 附近,返回【四舍五入】后的整数
- ceil 天花板,返回【大于】参数的最小整数
- floor 地板 ,返回【小于】参数的最大整数
- rint 返回【最接近】参数的整数,如果刚好居中,则取偶数
round、ceil、floor、rint 方法经典示例
System.out.println(Math.round(1.5f) + " " + Math.round(-1.5f));//2 【-1】
System.out.println(Math.round(1.3f) + " " + Math.round(-1.3f));//1 -1
System.out.println(Math.round(1.6f) + " " + Math.round(-1.6f) + "\n");//2 -2
//******************************************************************************************
System.out.println(Math.ceil(1.5f) + " " + Math.ceil(-1.5f));//2.0 -1.0
System.out.println(Math.ceil(1.3f) + " " + Math.ceil(-1.3f));//2.0 -1.0
System.out.println(Math.ceil(1.6f) + " " + Math.ceil(-1.6f) + "\n");//2.0 -1.0
//******************************************************************************************
System.out.println(Math.floor(1.5f) + " " + Math.floor(-1.5f));//1.0 -2.0
System.out.println(Math.floor(1.3f) + " " + Math.floor(-1.3f));//1.0 -2.0
System.out.println(Math.floor(1.6f) + " " + Math.floor(-1.6f)+ "\n");//1.0 -2.0
//******************************************************************************************
System.out.println(Math.rint(1.5f) + " " + Math.rint(-1.5f));//2.0 -2.0
System.out.println(Math.rint(1.3f) + " " + Math.rint(-1.3f));//1.0 -1.0
System.out.println(Math.rint(1.6f) + " " + Math.rint(-1.6f));//2.0 -2.0
普通函数
public static int abs(int a) {
return (a < 0) ? -a : a;
}
- 参数:magnitude - 提供结果数值的参数;sign - 提供结果符号的参数
- 返回:一个值,带有 magnitude 的数值, sign 的符号。
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
三角函数
static double cosh(double x) 返回 double 值的双曲线余弦。
static double tanh(double x) 返回 double 值的双曲线余弦。
static double atan(double a) 返回一个值的反正切;返回的角度范围在 -pi/2 到 pi/2 之间。
角度弧度
static double toDegrees(double angrad) 将用弧度表示的角转换为近似相等的用角度表示的角。 public static double toDegrees(double angrad) {
return angrad * 180.0 / PI;
}
public static double toRadians(double angdeg) {
return angdeg / 180.0 * PI;
}
指数、对数、平方根
static double exp(double a) 返回欧拉数 e 的 double 次幂的值。
static double expm1(double x) 返回 e^x -1。
static double hypot(double x, double y) 返回 sqrt(x^2 +y^2),没有中间溢出或下溢。
static double IEEEremainder(double f1, double f2) 按照 IEEE 754 标准的规定,对两个参数进行余数运算。
static double log(double a) 返回 double 值的自然对数(底数是 e)。
static double log10(double a) 返回 double 值的底数为 10 的对数。
static double log1p(double x) 返回参数与 1 之和的自然对数,等同于log(x+1) 。
static double sqrt(double a) 返回正确舍入的 double 值的正平方根。
其他函数
static int getExponent(float f) 返回 float 表示形式中使用的无偏指数。
static double ulp(double d) 返回参数的 ulp 大小。
static float ulp(float f) 返回参数的 ulp 大小。
1.8之后新增的方法
//Returns the sum of its arguments, throwing an exception if the result overflows an int.
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;
}
public static int subtractExact(int x, int y) {
int r = x - y;
// HD 2-12 Overflow iff the arguments have different signs 【and】 the sign of the result is different than the sign of x
if (((x ^ y) & (x ^ r)) < 0) throw new ArithmeticException("integer overflow");
return r;
}
public static int multiplyExact(int x, int y) {
long r = (long)x * (long)y;
if ((int)r != r) throw new ArithmeticException("integer overflow");
return (int)r;
}
public static int incrementExact(int a) {
if (a == Integer.MAX_VALUE) throw new ArithmeticException("integer overflow");
return a + 1;
}
public static int decrementExact(int a) {
if (a == Integer.MIN_VALUE) throw new ArithmeticException("integer overflow");
return a - 1;
}
public static int negateExact(int a) {
if (a == Integer.MIN_VALUE) throw new ArithmeticException("integer overflow");
return -a;
}
public static int toIntExact(long value) {
if ((int)value != value) throw new ArithmeticException("integer overflow");
return (int)value;
}
- Returns the largest (closest to positive infinity 最接近正无穷大) int value that is less than or equal to小于或等于 the algebraic代数 quotient商.
public static int floorDiv(int x, int y) {
int r = x / y;
// if the signs are different and modulo not zero, round down
if ((x ^ y) < 0 && (r * y != x)) r--;
return r;
}
Returns the floor modulus模 of the int
arguments.
The floor modulus is x - (floorDiv(x, y) * y)
, has the same sign符号 as the divisor除数 y
, and is in the range of -abs(y) < r < +abs(y)
.
The relationship关系 between floorDiv
and floorMod
is such that:
floorDiv(x, y) * y + floorMod(x, y) == x
The difference in values between floorMod
and the %
operator操作符 is due to由于 the difference between (floorDiv
that returns the integer less than or equal to the quotient商) and (the /
operator that returns the integer closest to zero).
floorMod和%操作符之间的值之间的差异是由于,【返回的小于或等于商的整数floorDiv】与【返回最接近零的整数的/运算符】之间的差异。
Examples:
- If the signs of the arguments are the same, the results of
floorMod
and the%
operator are the same. 如果参数的符号相同,则floorMod和%运算符的结果是相同的。
floorMod(4, 3) == 1
; and(4 % 3) == 1
- If the signs of the arguments are different, the results differ from the
%
operator. 如果参数的符号不同,则结果与%运算符不同。
floorMod(+4, -3) == -2
; and(+4 % -3) == +1
floorMod(-4, +3) == +2
; and(-4 % +3) == -1
floorMod(-4, -3) == -1
; and(-4 % -3) == -1
If the signs of arguments are unknown and a positive正 modulus is needed it can be computed as ( floorMod(x, y) + abs(y) ) % abs(y)
.
public static int floorMod(int x, int y) {
return x - floorDiv(x, y) * y;
}
- 这个方法语义上等同于nextAfter(d,Double.NEGATIVE_INFINITY); 然而,nextDown实现可能比其等同的nextAfter调用运行得更快。
public static double nextDown(double d) {
if (Double.isNaN(d) || d == Double.NEGATIVE_INFINITY) return d;
else {
if (d == 0.0) return -Double.MIN_VALUE;
else return Double.longBitsToDouble(Double.doubleToRawLongBits(d) + ((d > 0.0d)?-1L:+1L));
}
}
常用API使用演示
Math.abs(12.3); //12.3 返回这个数的绝对值
Math.abs(-12.3); //12.3
Math.copySign(1.23, -12.3); //-1.23,返回第一个参数的量值和第二个参数的符号
Math.copySign(-12.3, 1.23); //12.3
Math.signum(x); //如果x大于0则返回1.0,小于0则返回-1.0,等于0则返回0
Math.signum(12.3); //1.0
Math.signum(-12.3); //-1.0
Math.signum(0); //0.0
//指数
Math.exp(x); //e的x次幂
Math.expm1(x); //e的x次幂 - 1
Math.scalb(x, y); //x*(2的y次幂)
Math.scalb(12.3, 3); //12.3*2³
//取整
Math.ceil(12.3); //返回最近的且大于这个数的整数13.0
Math.ceil(-12.3); //-12.0
Math.floor(12.3); //返回最近的且小于这个数的整数12.0
Math.floor(-12.3); //-13.0
//x和y平方和的二次方根
Math.hypot(x, y); //√(x²+y²)
//返回概述的二次方根
Math.sqrt(x); //√(x) x的二次方根
Math.sqrt(9); //3.0
Math.sqrt(16); //4.0
//返回该数的立方根
Math.cbrt(27.0); //3
Math.cbrt(-125.0); //-5
//对数函数
Math.log(e); //1 以e为底的对数
Math.log10(100); //10 以10为底的对数
Math.log1p(x); //Ln(x+ 1)
//返回较大值和较小值
Math.max(x, y); //返回x、y中较大的那个数
Math.min(x, y); //返回x、y中较小的那个数
//返回 x的y次幂
Math.pow(x, y);
Math.pow(2, 3); //即2³ 即返回:8
//随机返回[0,1)之间的无符号double值
Math.random();
//返回最接近这个数的整数,如果刚好居中,则取偶数
Math.rint(12.3); //12.0
Math.rint(-12.3); //-12.0
Math.rint(78.9); //79.0
Math.rint(-78.9); //-79.0
Math.rint(34.5); //34.0
Math.rint(35.5); //36.0
Math.round(12.3); //与rint相似,返回值为long
//三角函数
Math.sin(α); //sin(α)的值
Math.cos(α); //cos(α)的值
Math.tan(α); //tan(α)的值
//求角
Math.asin(x/z); //返回角度值[-π/2,π/2] arc sin(x/z)
Math.acos(y/z); //返回角度值[0~π] arc cos(y/z)
Math.atan(y/x); //返回角度值[-π/2,π/2]
Math.atan2(y-y0, x-x0); //同上,返回经过点(x,y)与原点的的直线和经过点(x0,y0)与原点的直线之间所成的夹角
Math.sinh(x); //双曲正弦函数sinh(x)=(exp(x) - exp(-x)) / 2.0;
Math.cosh(x); //双曲余弦函数cosh(x)=(exp(x) + exp(-x)) / 2.0;
Math.tanh(x); //tanh(x) = sinh(x) / cosh(x);
//角度弧度互换
Math.toDegrees(angrad); //角度转换成弧度,返回:angrad * 180d / PI
Math.toRadians(angdeg); //弧度转换成角度,返回:angdeg / 180d * PI