这次主要整理下Java中Math类的常用方法
一、Math类的定义
- Math类位于 java.lang 包中,主要提供了一些常用的数学函数和计算
二、Math类的常用方法
1、三角函数运算
- Math.toDegrees(double angrad)
- 将弧度转换为角度
System.out.println(Math.toDegrees(1.5707963267948966)); // 90.0
- Math.toRadians(double angdeg)
- 将角度转换为弧度
System.out.println(Math.toRadians(90)); // 1.5707963267948966
- Math.sins(double a)
- 计算正弦值
// 先将30°转换为弧度,再计算其正弦值,30°的正弦值为0.5
System.out.println(Math.sin(Math.toRadians(30))); // 0.49999999999999994 约等于0.5
- Math.asin(double a)
- 计算反正弦值
// 先求出值为0.5的反正弦值(弧度),再将弧度转换为角度
System.out.println(Math.toDegrees(Math.asin(0.5))); // 30.000000000000004 约等于30°
- Math.cos(double a)
- 计算余弦值
// 先将60°转换为弧度,再计算其余弦值,60°的余弦值为0.5
System.out.println(Math.cos(Math.toRadians(60))); // 0.5000000000000001 约等于0.5
- Math.acos(double a)
- 计算反余弦值
// 先求出值为0.5的反余弦值(弧度),再将弧度转换为角度
System.out.println(Math.toDegrees(Math.acos(0.5))); // 60.00000000000001 约等于60°
- Math.tan(double a)
- 计算正切值
- 30°的正切值:
// 先将30°转换为弧度,再计算其正切值
System.out.println(Math.tan(Math.toRadians(30))); // 0.5773502691896257
- Math.atan(double a)
- 计算反正切值
// 先求出值为0.5的反正切值(弧度),再将弧度转换为角度
System.out.println(Math.toDegrees(Math.atan(0.5773502691896257))); // 29.999999999999996 约等于30°
- Math.sinh(double x)
- 计算双曲正弦值
- 双曲正弦值表达式:
System.out.println(Math.sinh(1)); // 1.1752011936438014
- Math.cosh(double x)
- 计算双曲余弦值
- 双曲余弦值表达式:
System.out.println(Math.cosh(1)); // 1.543080634815244
- Math.tanh(double x)
- 计算双曲正切值
- 双曲正切值表达式:
System.out.println(Math.tanh(1)); // 0.7615941559557649
- Math.atan2(double y, double x)
- 计算指定点与极坐标的弧度
- 指定点的y轴坐标为 ,x轴坐标为1,其与(0, 0)所成的角度为60°
System.out.println(Math.toDegrees(Math.atan2(Math.sqrt(3), 1))) // 59.99999999999999 约等于60°
2、算术运算
- Math.addExact(int x, int y)
- 计算两参数之和,参数类型:int或long
System.out.println(Math.addExact(1, 2)); // 3
- Math.subtractExact(int x, int y)
- 计算两参数之差(第一个参数 - 第二个参数),参数类型:int或long
System.out.println(Math.subtractExact(3, 2)); // 1
- Math.multiplyExact(int x, int y)
- 计算两参数的乘积,参数类型:int或long
System.out.println(Math.multiplyExact(2, 3)); // 6
- Math.floorMod(int x, int y)
- 计算两参数相除的余数(第一个参数 / 第二个参数),参数类型:int或long
System.out.println(Math.floorMod(2, 3)); // 2
- Math.floorDiv(int x, int y)
- 计算等于或小于商的最大整数值,参数类型:int或long
System.out.println(Math.floorDiv(4, 3)); // 1
- Math.pow(double a, double b)
- 计算a的b次幂,
System.out.println(Math.pow(2, 3)); // 8.0
- Math.exp(double a)
- 计算自然常数的a次幂,
System.out.println(Math.exp(1)); // 2.718281828459045
- Math.sqrt(double a)
- 计算平方根,
System.out.println(Math.sqrt(16)); // 4.0
- Math.cbrt(double a)
- 计算立方根,
System.out.println(Math.cbrt(8)); // 2.0
- Math.hypot(double x, double y)
- 计算两个参数平方和的平方根,
System.out.println(Math.hypot(6, 8)); // 10.0
- Math.log(double x)
- 计算以e为底的对数(自然对数), 或
System.out.println(Math.log(Math.E)); // 1.0
- Math.log10(double x)
- 计算以10为底的对数, 或
System.out.println(Math.log10(100)); // 2.0
- Math.log1p(double x)
- 计算以e为底,1与参数之和为指数的对数, 或
System.out.println(Math.log1p(Math.E - 1)); // 1.0
- Math.expm1(double x)
- 计算e的x次幂-1, 或
System.out.println(Math.expm1(1) + 1); // 2.718281828459045
- Math.scalb(float x, int y)
- 计算x乘以2的y次幂,
System.out.println(Math.scalb(2, 3)); // 16.0
- Math.IEEEremainder(double x, double y)
- 按照 IEEE 754 标准的规定,对两个参数进行余数运算,余数的算术值等于
- 其中 n 为最接近 的商的整数
- 如果两个整数同样接近,则选其中的偶数
- 如果余数为0,那么它的符号与第一个参数的符号相同
System.out.println(Math.IEEEremainder(105, 10)); // 5.0
System.out.println(Math.IEEEremainder(106, 10)); // -4.0
3、取整运算
- Math.ceil(double x)
- 向上取整,返回大于该值的最近double值
System.out.println(Math.ceil(1.23)); // 2.0
System.out.println(Math.ceil(-1.23)); // -1.0
- Math.floor(double x)
- 向下取整,返回小于该值的最近double值
System.out.println(Math.floor(1.23)); // 1.0
System.out.println(Math.floor(-1.23)); // -2.0
- Math.round(double x)
- 四舍五入取整,返回long值,参数类型:double或float
System.out.println(Math.round(1.43)); // 1
System.out.println(Math.round(1.53)); // 2
System.out.println(Math.round(-1.43)); // -1
System.out.println(Math.round(-1.53)); // -2
4、随机运算
- Math.random()
- 内部调用了 Random.nextDouble() 方法,生成一个伪均匀分布在0.0到1.0之间的double值
System.out.println((int)(Math.random() * 10 + 1)); // 生成范围再[1, 11)之间的伪随机数
5、符号运算
- Math.abs(int x)
- 计算绝对值,参数类型:int、long、float、double
System.out.println(Math.abs(-1)); // 1
- Math.negateExact(int x)
- 计算相反值,参数类型:int、long
System.out.println(Math.negateExact(-1)); // 1
- Math.signum(double x)
- 如果参数为 0.0,则返回 0.0;如果参数大于 0.0,则返回 1.0;如果参数小于 0.0,则返回 -1.0,参数类型:float、double
System.out.println(Math.signum(0.1)); // 1.0
System.out.println(Math.signum(0.0)); // 0.0
System.out.println(Math.signum(-0.1)); // -1.0
- Math.copySign(double magnitude, double sign)
- 获取带有第二个浮点参数符号的第一个浮点参数,参数类型:(double, double)、(float, float)
System.out.println(Math.copySign(1.1, -0.1)); // -1.1
6、大小运算
- Math.max(int x, int y)
- 获取两个参数中的最大值,参数类型:int、long、float、double
System.out.println(Math.max(1, 2)); // 2
- Math.min(int x, int y)
- 获取两个参数中的最小值,参数类型:int、long、float、double
System.out.println(Math.min(1, 2)); // 1
- Math.decrementExact(int x)
- 获取该参数递减之后的数,参数类型:int、long
System.out.println(Math.decrementExact(2)); // 1
- Math.incrementExact(int x)
- 获取该参数递增之后的数,参数类型:int、long
System.out.println(Math.incrementExact(1)); // 2
- Math.nextUp(double x)
- 获取比参数略大的相邻浮点数,参数类型:float、double
System.out.println(Math.nextUp(1.1)); // 1.1000000000000003
- Math.nextDown(double x)
- 获取比参数略小的相邻浮点数,参数类型:float、double
System.out.println(Math.nextDown(1.1)); // 1.0999999999999999
- Math.nextAfter(double start, double direction)
- 获取第一个参数和第二个参数之间与第一个参数相邻的浮点数,参数类型:(double, double)、(float, double)
System.out.println(Math.nextAfter(1.2, 1.3)); // 1.2000000000000002
- Math.rint(double x)
- 获取与参数最接近的double值
System.out.println(Math.rint(1.4)); // 1.0
System.out.println(Math.rint(1.5)); // 2.0
System.out.println(Math.rint(-1.4)); // -1.0
System.out.println(Math.rint(-1.5)); // -2.0
7、其他运算
- Math.ulp(double x)
- 获取参数的ulp值,ulp值是该浮点值与下一个数值较大浮点值之间的正距离,参数类型:float、double
System.out.println(Math.ulp(0.1)); // 1.3877787807814457E-17
- Math.getExponent(double x)
- 获取在表示浮点数时使用的无偏指数,参数类型:float、double
- 该方法的具体含义还不是很明白,如果有了解的小伙伴,可以在评论区解释一下 ╰( ̄▽ ̄)╭
System.out.println(Math.getExponent(1024)); // 10