在Java中,Math
类位于 java.lang
包中,提供了许多用于执行基本数学运算的静态方法。这些方法涵盖了从基本的算术运算到复杂的三角函数和指数运算。以下是对 Math
类常用方法的详细介绍:
1. 常量
-
Math.PI
表示圆周率 π 的近似值,约为 3.141592653589793。
double pi = Math.PI;
-
Math.E
表示自然对数的底数 e,约为 2.718281828459045。
double e = Math.E;
2. 绝对值
-
abs
方法返回参数的绝对值。
int absInt = Math.abs(-10); // 10 double absDouble = Math.abs(-5.5); // 5.5
3. 最大值与最小值
-
max
方法返回两个参数中的较大值。
int maxInt = Math.max(10, 20); // 20 double maxDouble = Math.max(5.5, 2.3); // 5.5
-
min
方法返回两个参数中的较小值。
int minInt = Math.min(10, 20); // 10 double minDouble = Math.min(5.5, 2.3); // 2.3
4. 幂运算与平方根
-
pow
方法返回第一个参数的第二个参数次幂。
double power = Math.pow(2, 3); // 8.0
-
sqrt
方法返回参数的平方根。
double squareRoot = Math.sqrt(16); // 4.0
5. 指数与对数
-
exp
方法返回 e 的参数次幂。
double exponential = Math.exp(1); // e ≈ 2.718281828459045
-
log
方法返回参数的自然对数(以 e 为底)。
double naturalLog = Math.log(Math.E); // 1.0
-
log10
方法返回参数的常用对数(以 10 为底)。
double logBase10 = Math.log10(100); // 2.0
6. 三角函数
-
sin
、cos
、tan
方法计算给定弧度的正弦、余弦和正切值。
double sine = Math.sin(Math.PI / 2); // 1.0 double cosine = Math.cos(0); // 1.0 double tangent = Math.tan(Math.PI / 4); // 1.0
-
asin
、acos
、atan
方法计算反正弦、反余弦和反正切值,返回值为弧度。
double arcsine = Math.asin(1.0); // π/2 double arccosine = Math.acos(1.0); // 0.0 double arctangent = Math.atan(1.0); // π/4
-
toDegrees
和toRadians
方法在度和弧度之间转换。
double degrees = Math.toDegrees(Math.PI); // 180.0 double radians = Math.toRadians(180); // π
7. 取整方法
-
ceil
方法返回大于或等于参数的最小整数(以
double
形式返回)。double ceiling = Math.ceil(4.2); // 5.0
-
floor
方法返回小于或等于参数的最大整数(以
double
形式返回)。double floor = Math.floor(4.8); // 4.0
-
round
方法返回四舍五入后的整数值,类型根据参数不同而不同(
int
或long
)。long rounded = Math.round(4.5); // 5 int roundedInt = Math.round(4.4f); // 4
-
rint
方法返回最接近参数的整数值,作为
double
返回。如果参数恰好在两个整数之间,返回偶数。double rintValue = Math.rint(4.5); // 4.0
8. 随机数
-
random
方法返回一个
double
类型的伪随机数,范围在[0.0, 1.0)
之间。double randomValue = Math.random();
示例:生成 1 到 100 之间的随机整数
int randomInt = (int)(Math.random() * 100) + 1;
9. 其他有用的方法
-
hypot
方法计算直角三角形的斜边长度,避免溢出或下溢。
double hypotenuse = Math.hypot(3, 4); // 5.0
-
signum
方法返回参数的符号函数值:-1.0、0.0 或 1.0。
double sign = Math.signum(-5.5); // -1.0
-
copySign
方法返回第一个参数的绝对值,并赋予第二个参数的符号。
double result = Math.copySign(5.0, -3.0); // -5.0
-
nextAfter
和nextUp
、nextDown
方法获取紧邻指定方向的浮点数。
double next = Math.nextAfter(1.0, 2.0); // 1.0000000000000002 double nextUp = Math.nextUp(1.0); // 1.0000000000000002 double nextDown = Math.nextDown(1.0); // 0.9999999999999999
-
ulp
方法返回指定浮点数的单位的最后一位(ULP)。
double ulpValue = Math.ulp(1.0); // 2.220446049250313E-16
10. 高级数学运算
-
atan2
方法计算给定 y 和 x 坐标的点相对于原点的角度(弧度)。
double angle = Math.atan2(1.0, 1.0); // π/4
-
sinh
、cosh
、tanh
方法计算双曲正弦、双曲余弦和双曲正切。
double sinhValue = Math.sinh(1.0); double coshValue = Math.cosh(1.0); double tanhValue = Math.tanh(1.0);
-
expm1
方法计算
e^x - 1
,在 x 逼近 0 时更精确。double expm1Value = Math.expm1(1.0); // e - 1 ≈ 1.718281828459045
-
log1p
方法计算
ln(1 + x)
,在 x 逼近 0 时更精确。double log1pValue = Math.log1p(1.0); // ln(2) ≈ 0.6931471805599453
示例代码
以下是一个综合示例,展示了如何使用 Math
类的一些方法:
public class MathExample {
public static void main(String[] args) {
// 常量
System.out.println("PI: " + Math.PI);
System.out.println("E: " + Math.E);
// 绝对值
System.out.println("Abs of -10: " + Math.abs(-10));
// 最大值与最小值
System.out.println("Max of 10 and 20: " + Math.max(10, 20));
System.out.println("Min of 10 and 20: " + Math.min(10, 20));
// 幂运算与平方根
System.out.println("2^3: " + Math.pow(2, 3));
System.out.println("Square root of 16: " + Math.sqrt(16));
// 指数与对数
System.out.println("exp(1): " + Math.exp(1));
System.out.println("log(E): " + Math.log(Math.E));
System.out.println("log10(100): " + Math.log10(100));
// 三角函数
System.out.println("sin(π/2): " + Math.sin(Math.PI / 2));
System.out.println("cos(0): " + Math.cos(0));
System.out.println("tan(π/4): " + Math.tan(Math.PI / 4));
// 取整方法
System.out.println("Ceil of 4.2: " + Math.ceil(4.2));
System.out.println("Floor of 4.8: " + Math.floor(4.8));
System.out.println("Round of 4.5: " + Math.round(4.5));
// 随机数
System.out.println("Random number: " + Math.random());
// 高级运算
System.out.println("Hypotenuse of 3 and 4: " + Math.hypot(3, 4));
System.out.println("Signum of -5.5: " + Math.signum(-5.5));
System.out.println("Next after 1.0 towards 2.0: " + Math.nextAfter(1.0, 2.0));
}
}
输出示例:
PI: 3.141592653589793
E: 2.718281828459045
Abs of -10: 10
Max of 10 and 20: 20
Min of 10 and 20: 10
2^3: 8.0
Square root of 16: 4.0
exp(1): 2.718281828459045
log(E): 1.0
log10(100): 2.0
sin(π/2): 1.0
cos(0): 1.0
tan(π/4): 0.9999999999999999
Ceil of 4.2: 5.0
Floor of 4.8: 4.0
Round of 4.5: 5
Random number: 0.3746494481042324
Hypotenuse of 3 and 4: 5.0
Signum of -5.5: -1.0
Next after 1.0 towards 2.0: 1.0000000000000002