文章目录
- 89. Java 数字和字符串 - Math 类深入解析
- 一、引言
- 二、常量与基本方法
- 2.1 Math 类常量
- 2.2 绝对值和舍入
- 绝对值方法
- 舍入方法
- 最小值和最大值
- 三、指数与对数方法
- 四、三角函数方法
- 五、总结
89. Java 数字和字符串 - Math 类深入解析
一、引言
在 Java 中,除了基本的加、减、乘、除和求余运算符之外,java.lang.Math
类为我们提供了大量静态方法和常量,用于执行高级数学计算。由于 Math
类中的所有方法均为静态方法,我们可以直接通过类名调用它们,而不必实例化 Math
类。此外,通过静态导入(import static java.lang.Math.*;
)还可以直接使用方法名称。
二、常量与基本方法
2.1 Math 类常量
Math 类内置两个重要常量:
Math.E
:自然对数的底数,约为2.7183
Math.PI
:圆周率,约为3.1416
2.2 绝对值和舍入
绝对值方法
Math
提供了不同类型的 abs()
方法,将传入的值转换为其绝对值:
double abs(double d)
float abs(float f)
int abs(int i)
long abs(long lng)
示例:
double a = -191.635;
System.out.printf("The absolute value of %.3f is %.3f%n", a, Math.abs(a));
// 输出: The absolute value of -191.635 is 191.635
舍入方法
Math 类中提供了多种舍入方法:
ceil(double d)
:返回大于或等于d
的最小整数,结果为double
类型。floor(double d)
:返回小于或等于d
的最大整数,结果为double
类型。rint(double d)
:返回与d
最接近的整数,采用银行家舍入法(四舍六入五考虑)。round(double d)
或round(float f)
:返回最接近d
的整数,结果分别为long
或int
类型。
示例:
double b = 43.74;
System.out.printf("The ceiling of %.2f is %.0f%n", b, Math.ceil(b));
System.out.printf("The floor of %.2f is %.0f%n", b, Math.floor(b));
System.out.printf("The rint of %.2f is %.0f%n", b, Math.rint(b));
输出:
The ceiling of 43.74 is 44
The floor of 43.74 is 43
The rint of 43.74 is 44
最小值和最大值
Math 类提供了用于比较两个数字大小的静态方法:
- min() 方法:返回两个参数中的较小值。
- max() 方法:返回两个参数中的较大值。
示例:
int c = 16, d = 45;
System.out.printf("The max of %d and %d is %d%n", c, d, Math.max(c, d));
System.out.printf("The min of %d and %d is %d%n", c, d, Math.min(c, d));
输出:
The max of 16 and 45 is 45
The min of 16 and 45 is 16
三、指数与对数方法
Math 类中还提供了处理指数、对数和幂运算的方法,这在科学计算中非常常用。
- exp(double d):计算 e 的 d 次幂。
- log(double d):计算 d 的自然对数。
- pow(double base, double exponent):计算
base
的exponent
次幂。 - sqrt(double d):计算 d 的平方根。
示例程序(ExponentialDemo
):
public class ExponentialDemo {public static void main(String[] args) {double x = 11.635;double y = 2.76;System.out.printf("The value of e is %.4f%n", Math.E);System.out.printf("exp(%.3f) is %.3f%n", x, Math.exp(x));System.out.printf("log(%.3f) is %.3f%n", x, Math.log(x));System.out.printf("pow(%.3f, %.3f) is %.3f%n", x, y, Math.pow(x, y));System.out.printf("sqrt(%.3f) is %.3f%n", x, Math.sqrt(x));}
}
示例输出:
The value of e is 2.7183
exp(11.635) is 112983.831
log(11.635) is 2.454
pow(11.635, 2.760) is 874.008
sqrt(11.635) is 3.411
四、三角函数方法
Math 类还提供了一系列三角函数,这些方法均以弧度为单位进行计算。如果需要将角度转换为弧度,可使用 toRadians(double d)
;反之,则使用 toDegrees(double d)
。
主要方法包括:
- sin(double d):计算正弦值。
- cos(double d):计算余弦值。
- tan(double d):计算正切值。
asin(double d)
、acos(double d)
、atan(double d)
:反三角函数,返回角度的弧度值。atan2(double y, double x)
:根据直角坐标 (x, y) 返回极角(弧度)。
示例程序(TrigonometricDemo
):
public class TrigonometricDemo {public static void main(String[] args) {double degrees = 45.0;double radians = Math.toRadians(degrees);System.out.format("The value of pi is %.4f%n", Math.PI);System.out.format("The sine of %.1f degrees is %.4f%n", degrees, Math.sin(radians));System.out.format("The cosine of %.1f degrees is %.4f%n", degrees, Math.cos(radians));System.out.format("The tangent of %.1f degrees is %.4f%n", degrees, Math.tan(radians));System.out.format("The arcsine of %.4f is %.4f degrees%n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));System.out.format("The arccosine of %.4f is %.4f degrees%n", Math.cos(radians), Math.toDegrees(Math.acos(Math.cos(radians))));System.out.format("The arctangent of %.4f is %.4f degrees%n", Math.tan(radians), Math.toDegrees(Math.atan(Math.tan(radians))));}
}
示例输出(部分):
The value of pi is 3.1416
The sine of 45.0 degrees is 0.7071
The cosine of 45.0 degrees is 0.7071
The tangent of 45.0 degrees is 1.0000
The arcsine of 0.7071 is 45.0000 degrees
The arccosine of 0.7071 is 45.0000 degrees
The arctangent of 1.0000 is 45.0000 degrees
五、总结
- Math 类简介:提供基本算术之外的高级数学方法,所有方法均为静态调用。
- 常量:
Math.E
和Math.PI
为常用常量。 - 绝对值、舍入、最值方法:使用
abs、ceil、floor、rint、round、min、max
等方法进行数值处理。 - 指数与对数:通过
exp、log、pow、sqrt
方法实现幂和对数计算。 - 三角函数:支持
sin、cos、tan
以及反三角函数,角度与弧度可相互转换。
希望这份讲义能够帮助大家深入理解 Math 类的各项功能,并在实际编程中加以灵活运用!