文章目錄
- 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 類的各項功能,并在實際編程中加以靈活運用!