Java Math方法记录
Java 提供 java.lang.Math 类,很方便的进行数学运算。
Math 类是基于浮点数的运算,可能导致精度损失,不适用于高精度计算。
记录如下
常量
提供了两个常量,
Math.PI
:圆周率πMath.E
:自然对数的底数 e
System.out.println("圆周率 π : " + Math.PI);
System.out.println("自然对数的底数 e : " + Math.E);
圆周率 π : 3.141592653589793
自然对数的底数 e : 2.718281828459045
取值(最大值、最小值、绝对值、取反)
Math.max(int a, int b)
:取 a 、b 中的最大值。有重载方法,还支持 double 、float 、long 型参数。Math.min(int a, int b)
:取 a 、b 中的最小值。有重载方法,还支持 double 、float 、long 型参数。Math.abs(int a)
:取 a 的绝对值。有重载方法,还支持 double 、float 、long 型参数。Math.negateExact(int a)
:对 a 取反。有重载方法,还支持 long 型参数。
System.out.println("取最大值,示例 Math.max(100,200) : " + Math.max(100,200));
System.out.println("取最小值,示例 Math.min(100,200) : " + Math.min(100,200));
System.out.println("取绝对值,示例 Math.abs(-100) : " + Math.abs(-100));
System.out.println("取反,示例 Math.negateExact(100) : " + Math.negateExact(100));
取最大值,示例 Math.max(100,200) : 200
取最小值,示例 Math.min(100,200) : 100
取绝对值,示例 Math.abs(-100) : 100
取反,示例 Math.negateExact(100) : -100
四舍五入
Math.round(double a)
:对 a 四舍五入。有重载方法,还支持 float 型参数。
System.out.println("2023.4 四舍五入 : " + Math.round(2023.4));
System.out.println("2023.5 四舍五入 : " + Math.round(2023.5));
2023.4 四舍五入 : 2023
2023.5 四舍五入 : 2024
取整
Math.rint(double a)
: 返回最接近 a 的整数,如果有2个这样的整数,就返回其中的偶数。Math.ceil(double a)
:向上取整。Math.floor(double a)
:向下取整。
System.out.println("取最接近的整数 Math.rint(-2.5) : " + Math.rint(-2.1));System.out.println("取最接近的整数 Math.rint(-2.9) : " + Math.rint(-2.9));System.out.println("取最接近的整数 Math.rint(3.2) : " + Math.rint(3.2));System.out.println("取最接近的整数 Math.rint(4.7) : " + Math.rint(4.7));System.out.println("取最接近的整数 Math.rint(5.5) : " + Math.rint(5.5));System.out.println("99.5 向上取整 Math.ceil(99.5) : " + Math.ceil(99.5));System.out.println("99.5 向下取整 Math.floor(99.5) : " + Math.floor(99.5));System.out.println("-99.5 向上取整 Math.floor(-99.5) : " + Math.floor(-99.5));
取最接近的整数 Math.rint(-2.5) : -2.0
取最接近的整数 Math.rint(-2.9) : -3.0
取最接近的整数 Math.rint(3.2) : 3.0
取最接近的整数 Math.rint(4.7) : 5.0
取最接近的整数 Math.rint(5.5) : 6.0
99.5 向上取整 Math.ceil(99.5) : 100.0
99.5 向下取整 Math.floor(99.5) : 99.0
-99.5 向下取整 Math.floor(-99.5) : -100.0
加减乘除
Math.addExact(int x, int y)
:相加,有重载方法,还支持 long 型参数。Math.subtractExact(int x, int y)
:相减,返回 x - y 的值。有重载方法,还支持 long 型参数。Math.multiplyExact(int x, int y)
:相乘。有重载方法,还支持 long 型参数。Math.floorDiv(int x, int y)
:除法,返回 x / y 的值,返回值向下取整。即 查找小于或等于代数商的最大整数值。
System.out.println("加法,Math.addExact(4,5) : " + Math.addExact(4,5));System.out.println("减法,Math.subtractExact(10,7) : " + Math.subtractExact(10,7));System.out.println("乘法:Math.multiplyExact(3,7) : " + Math.multiplyExact(3,7));System.out.println("除法:Math.floorDiv(6,3) : " + Math.floorDiv(6,3));System.out.println("除法:Math.floorDiv(10L,3L) : " + Math.floorDiv(10L,3L));System.out.println("除法:Math.floorDiv(10,3) : " + Math.floorDiv(10,3));System.out.println("除法:Math.floorDiv(14L,5L) : " + Math.floorDiv(14L,5L));System.out.println("除法:Math.floorDiv(14,5) : " + Math.floorDiv(14,5));System.out.println("除法:14/5 : " + 14/5);System.out.println("除法:(float) 14/5 : " + (float)14/5);
加法,Math.addExact(4,5) : 9
减法,Math.subtractExact(10,7) : 3
乘法:Math.multiplyExact(3,7) : 21
除法:Math.floorDiv(6,3) : 2
除法:Math.floorDiv(10L,3L) : 3
除法:Math.floorDiv(10,3) : 3
除法:Math.floorDiv(14L,5L) : 2
除法:Math.floorDiv(14,5) : 2
除法:14/5 : 2
除法:(float) 14/5 : 2.8
取余
Math.IEEEremainder(double f1, double f2)
: 采用 IEEE 754 标准 ,f1 对 f2 取余 。
Math.IEEEremainder() 方法和 % 运算符返回的余数值等于 arg1-arg2 * n。但是, n 的值是不同的。
IEEEremainder() 方法 :n 是最接近 arg1/arg2 的整数,如果 arg1/arg2 返回一个介于两个整数之间的值,则 n 是偶数整数,即偶数取整。
% 运算符 :n 是 arg1/arg2 的整数部分,即向下取整。
举个例子,9 对 5 取余,
IEEEremainder() 方法 : n = 9/5 =1.8 ,取 2 。余数 = 9 - 5 * 2 = -1 。
% 运算符 :n = 9/5 = 1.8 , 取 1 。余数 = 9 - 5 * 1 = 4 。
System.out.println("求10/3的余数,采用IEEE 754标准,Math.IEEEremainder(9,5): " + Math.IEEEremainder(9,5));
System.out.println("求10/3的余数 9%5: " + 9%5);
求9/5的余数,采用IEEE 754标准,Math.IEEEremainder(9,5): -1.0
求9/5的余数 9%5: 4
取模
-
Math.floorMod(int x, int y)
:取模。有重载方法,还支持 long 型参数。返回值是 x - (floorDiv(x, y) * y) ,和除数 y 具有相同的符号 ,介于 -|y| < r < |y| 之间。
Math.floorMod() 方法 和 Math.floorDiv() 方法的关系是 : floorDiv(x, y) * y + floorMod(x, y) == x 。
Math.floorMod() 方法和 % 运算符的差异 :
如果 x 和 y 的符号相同,两者结果一致。
floorMod(4, 3) == 1; and (4 % 3) == 1
如果 x 和 y 的符号不相同,两者结果不一致。
floorMod(+4, -3) == -2; and (+4 % -3) == +1
floorMod(-4, +3) == +2; and (-4 % +3) == -1
floorMod(-4, -3) == -1; and (-4 % -3) == -1
随机数
Math.random()
:生成一个大于等于 0.0 且小于 1.0 的 double 型随机数。
System.out.println("生成一个随机数 Math.random() : " + Math.random());
生成一个随机数 Math.random() : 0.06326208314404602
幂次运算
Math.pow(double a, double b)
:求 a 的 b 次方。Math.exp(double a)
:求自然对数的底数 e 的 a 次方。
System.out.println("求2的3次方 Math.pow(2,3): " + Math.pow(2,3));
System.out.println("求自然对数的底数 e 的2次方 : " + Math.exp(2));
求2的3次方 Math.pow(2,3): 8.0
求自然对数的底数 e 的2次方 : 7.38905609893065
根号运算
Math.sqrt(double a)
:求 a 的平方根。Math.cbrt(double a)
:求 a 的立方根。
System.out.println("求25的平方根 Math.sqrt(25): " + Math.sqrt(25));
System.out.println("求27的立方根 Math.cbrt(27): " + Math.cbrt(27));
求25的平方根 Math.sqrt(25): 5.0
求27的立方根 Math.cbrt(27): 3.0
对数运算
Math.log(double a)
:求以 e 底 a 的对数。Math.log10(double a)
:求以 10 底 a 的对数。- 如果要计算以 a 为底 b 的对数,通过数学运算实现 :
Math.log(b)/Math.log(a)
System.out.println("求以e底e²的对数 Math.log(Math.E * Math.E) : " + Math.log(Math.E * Math.E));
System.out.println("求以10底100的对数 Math.log10(100) : " + Math.log10(100));
System.out.println("求以3为底81的对数 Math.log3(81) : " + Math.log(81)/Math.log(3));
求以e底e²的对数 Math.log(Math.E * Math.E) : 2.0
求以10底100的对数 Math.log10(100) : 2.0
求以3为底81的对数 Math.log3(81) : 4.0
弧度、角度运算
Math.toRadians(double angdeg)
: 把角度转换为弧度Math.toDegrees(double angrad)
:把弧度转换为角度
System.out.println("将角度转换为弧度 Math.toRadians(180) : " + Math.toRadians(180));//弧度 = 弧长 / 半径
System.out.println("将弧度转换为角度 Math.toRadians(3.14) : " + Math.toDegrees(3.14));
将角度转换为弧度 Math.toRadians(180) : 3.141592653589793
将弧度转换为角度 Math.toRadians(3.14) : 179.90874767107852
三角函数
Math.sin(double a)
:求角度的正弦函数值,非法输入则返回 NaNMath.asin(double a)
:求角度的余割函数值 (余割函数和正弦函数互为倒数),非法输入则返回 NaNMath.cos(double a)
:求角度的余弦函数值,非法输入则返回 NaNMath.acos(double a)
:求角度的正割函数值(正割函数和余弦函数互为倒数),非法输入则返回 NaNMath.tan(double a)
:求角度的正切函数值 ,非法输入则返回 NaNMath.atan(double a)
:求角度余切函数值 (余切函数和正切函数互为倒数),非法输入则返回 NaN
System.out.println("正弦 sin 90° ,Math.sin(90) : " + Math.sin(Math.toRadians(90)));
System.out.println("余割 csc 90° ,Math.asin(90) : " + Math.asin(Math.toRadians(90)));
System.out.println("余弦 cos 180° ,Math.cos(180) : " + Math.cos(Math.toRadians(180)));
System.out.println("正割 sec 180° ,Math.acos(180) : " + Math.acos(Math.toRadians(180)));
System.out.println("正切 tan 45° ,Math.tan(45) : " + Math.tan(Math.toRadians(45)));
System.out.println("余切 cot 45° ,Math.atan(45) : " + Math.atan(Math.toRadians(45)));
正弦 sin 90° ,Math.sin(90) : 1.0
余割 csc 90° ,Math.asin(90) : NaN
余弦 cos 180° ,Math.cos(180) : -1.0
正割 sec 180° ,Math.acos(180) : NaN
正切 tan 45° ,Math.tan(45) : 0.9999999999999999
余切 cot 45° ,Math.atan(45) : 0.6657737500283538
都知道 tan 45° = 1 ,输出的值是 0.9999999999999999 ,说明精度不准确。