在 Java 编程里,Math 类是一个极其重要的工具类,它存于 java.lang 包下。java.lang 包是 Java 语言的核心包,在使用其中的类时,无需额外导入,Java 编译器会自动处理。
Math 类有两个显著特点:
Math.PI 代表圆周率 π,其近似值为 3.141592653589793。在处理与圆、球等几何图形相关的计算时,这个常量非常有用。
public class PiExample {
public static void main(String[] args) {
// 定义圆的半径
double radius = 5.0;
// 计算圆的周长,公式为 C = 2 * π * r
double circumference = 2 * Math.PI * radius;
System.out.println("半径为 " + radius + " 的圆的周长是: " + circumference);
// 计算圆的面积,公式为 S = π * r * r
double area = Math.PI * radius * radius;
System.out.println("半径为 " + radius + " 的圆的面积是: " + area);
}
}
Math.E 表示自然对数的底数 e,近似值是 2.718281828459045。在涉及指数、对数等数学计算时会用到它。
public class EExample {
public static void main(String[] args) {
// 计算 e 的 3 次方
double power = Math.pow(Math.E, 3);
System.out.println("e 的 3 次方是: " + power);
// 计算以 e 为底 10 的对数
double log = Math.log(10);
System.out.println("以 e 为底 10 的对数是: " + log);
}
}
三角函数在数学和物理计算中经常出现,Math 类提供了计算正弦、余弦、正切的方法,这些方法的参数都是以弧度为单位的角度。
Math.sin(double a)
:该方法返回指定角度(以弧度为单位)的正弦值。Math.cos(double a)
:返回指定角度(以弧度为单位)的余弦值。Math.tan(double a)
:返回指定角度(以弧度为单位)的正切值。public class TrigonometricExample {
public static void main(String[] args) {
// 将 45 度转换为弧度,因为 Math 类的三角函数方法参数是弧度制
double angleInRadians = Math.toRadians(45);
// 计算正弦值
double sinValue = Math.sin(angleInRadians);
System.out.println("45 度的正弦值是: " + sinValue);
// 计算余弦值
double cosValue = Math.cos(angleInRadians);
System.out.println("45 度的余弦值是: " + cosValue);
// 计算正切值
double tanValue = Math.tan(angleInRadians);
System.out.println("45 度的正切值是: " + tanValue);
}
}
反三角函数用于根据三角函数值求对应的角度,返回值是弧度制。
Math.asin(double a)
:返回指定值的反正弦值,结果在 [- π 2 \frac{π}{2} 2π, π 2 \frac{π}{2} 2π] 范围内。Math.acos(double a)
:返回指定值的反余弦值,结果在 [0,π] 范围内。Math.atan(double a)
:返回指定值的反正切值,结果在 [- π 2 \frac{π}{2} 2π, π 2 \frac{π}{2} 2π] 范围内。public class InverseTrigonometricExample {
public static void main(String[] args) {
// 定义一个值
double value = 0.5;
// 计算反正弦值
double asinValue = Math.asin(value);
// 将弧度转换为度
double asinDegrees = Math.toDegrees(asinValue);
System.out.println("0.5 的反正弦值(度)是: " + asinDegrees);
// 计算反余弦值
double acosValue = Math.acos(value);
double acosDegrees = Math.toDegrees(acosValue);
System.out.println("0.5 的反余弦值(度)是: " + acosDegrees);
// 计算反正切值
double atanValue = Math.atan(value);
double atanDegrees = Math.toDegrees(atanValue);
System.out.println("0.5 的反正切值(度)是: " + atanDegrees);
}
}
Math.exp(double a)
:返回 e 的指定次幂,即 ea。Math.log(double a)
:返回指定值的自然对数(以 e 为底)。Math.log10(double a)
:返回指定值的常用对数(以 10 为底)。Math.pow(double a, double b)
:返回第一个参数的第二个参数次幂,即 ab。Math.sqrt(double a)
:返回指定值的平方根。public class ExponentAndLogExample {
public static void main(String[] args) {
// 计算 e 的 2 次方
double expResult = Math.exp(2);
System.out.println("e 的 2 次方是: " + expResult);
// 计算以 e 为底 10 的对数
double logResult = Math.log(10);
System.out.println("以 e 为底 10 的对数是: " + logResult);
// 计算以 10 为底 100 的对数
double log10Result = Math.log10(100);
System.out.println("以 10 为底 100 的对数是: " + log10Result);
// 计算 2 的 3 次方
double powerResult = Math.pow(2, 3);
System.out.println("2 的 3 次方是: " + powerResult);
// 计算 25 的平方根
double sqrtResult = Math.sqrt(25);
System.out.println("25 的平方根是: " + sqrtResult);
}
}
Math.ceil(double a)
:返回大于或等于指定数的最小整数,结果为 double 类型。可以理解为 “向上取整”,就像把一个小数 “推” 到比它大的最近的整数位置。Math.floor(double a)
:返回小于或等于指定数的最大整数,结果为 double 类型。也就是 “向下取整”,把小数 “拉” 到比它小的最近的整数位置。Math.rint(double a)
:返回最接近参数的整数,如果两个整数同样接近,则返回偶数。结果为 double 类型。Math.round(float a)
:返回最接近参数的 int 类型整数,四舍五入。Math.round(double a)
:返回最接近参数的 long 类型整数,四舍五入。public class RoundingExample {
public static void main(String[] args) {
double num = 3.7;
// 向上取整
double ceilValue = Math.ceil(num);
System.out.println(num + " 向上取整的结果是: " + ceilValue);
// 向下取整
double floorValue = Math.floor(num);
System.out.println(num + " 向下取整的结果是: " + floorValue);
// 返回最接近的整数(若两个同样接近则返回偶数)
double rintValue = Math.rint(num);
System.out.println(num + " 最接近的整数(规则处理)是: " + rintValue);
// 四舍五入为 int 类型
int roundInt = Math.round((float) num);
System.out.println(num + " 四舍五入为 int 类型的结果是: " + roundInt);
// 四舍五入为 long 类型
long roundLong = Math.round(num);
System.out.println(num + " 四舍五入为 long 类型的结果是: " + roundLong);
}
}
Math.abs(int a)
:返回整数的绝对值,还有 abs(long a)、abs(float a)、abs(double a) 等重载方法,分别用于处理不同数据类型的绝对值计算。Math.max(int a, int b)
:返回两个整数中的较大值,同样有 max(long a, long b)、max(float a, float b)、max(double a, double b) 等重载形式。Math.min(int a, int b)
:返回两个整数中的较小值,也有多种重载形式。public class AbsMaxMinExample {
public static void main(String[] args) {
int num1 = -5;
int num2 = 10;
// 计算绝对值
int absValue = Math.abs(num1);
System.out.println(num1 + " 的绝对值是: " + absValue);
// 找出较大值
int maxValue = Math.max(num1, num2);
System.out.println(num1 + " 和 " + num2 + " 中的较大值是: " + maxValue);
// 找出较小值
int minValue = Math.min(num1, num2);
System.out.println(num1 + " 和 " + num2 + " 中的较小值是: " + minValue);
}
}
Math.random()
:返回一个大于等于 0.0 且小于 1.0 的随机 double 类型数。可以通过一些简单的数学运算来生成指定范围内的随机数。public class RandomExample {
public static void main(String[] args) {
// 生成 1 到 10 之间的随机整数
int randomInt = (int) (Math.random() * 10 + 1);
System.out.println("1 到 10 之间的随机整数是: " + randomInt);
// 生成 20 到 30 之间的随机整数
int randomInRange = (int) (Math.random() * 11 + 20);
System.out.println("20 到 30 之间的随机整数是: " + randomInRange);
}
}
Math.toRadians(double angdeg)
:将角度转换为弧度。因为 Math 类的三角函数方法要求参数是弧度制,所以在使用角度进行三角函数计算时,需要先将角度转换为弧度。Math.toDegrees(double angrad)
:将弧度转换为角度。当通过反三角函数得到的结果是弧度制,而我们需要以度为单位表示时,就可以使用这个方法。public class AngleConversionExample {
public static void main(String[] args) {
// 将 60 度转换为弧度
double radians = Math.toRadians(60);
System.out.println("60 度转换为弧度是: " + radians);
// 将 π/3 弧度转换为度
double degrees = Math.toDegrees(Math.PI / 3);
System.out.println("π/3 弧度转换为度是: " + degrees);
}
}