在本文中,我們將簡要概述 Java 中的 Math 類別。讓我們來談談這個類別的方法以及如何使用它們。Math 類別位於 java.lang 套件中,並提供一組靜態方法來執行許多不同的數學計算。以下是 Math 類別可能有用的計算範例:
- 絕對值的計算(模值)
- 三角函數值的計算(正弦、餘弦等)
- 不同程度的海拔
- 提取不同程度的根
- 隨機數生成
- 四捨五入
- ETC。
- 靜態雙腹肌(雙a)
- 靜態浮動絕對值(浮動a)
- 靜態 int 絕對值(int a)
- 靜態長腹肌(長a)
public static void main(String[] args) {
System.out.println(Math.abs(-1)); // 1
System.out.println(Math.abs(-21.8d)); // 21.8
System.out.println(Math.abs(4532L)); // 4532
System.out.println(Math.abs(5.341f)); // 5.341
}
計算三角函數的值
Math 類別可讓您計算各種三角函數 - 正弦、餘弦、正切等。完整的方法清單可在官方文件網站上找到。下面是這些方法的清單:- 靜態雙罪(雙a)
- 靜態雙 cos(雙 a)
- 靜態雙tan(雙a)
- 靜態雙asin(雙a)
- 靜態雙 acos(雙 a)
- 靜態雙 atan(雙 a)
- 靜態雙 toDegrees(雙 angrad)
- 靜態雙 toRadians(雙 angdeg)
public static void main(String[] args) {
System.out.println(Math.sin(Math.toRadians(0)));
System.out.println(Math.sin(Math.toRadians(30)));
System.out.println(Math.sin(Math.toRadians(90)));
System.out.println(Math.cos(Math.toRadians(0)));
System.out.println(Math.cos(Math.toRadians(30)));
System.out.println(Math.cos(Math.toRadians(90)));
}
程式將輸出:
0.0
0.49999999999999994
1.0
1.0
0.8660254037844387
6.123233995736766E-17
它與正弦和餘弦表不太對應,部分原因是從度數轉換為弧度時出現錯誤。
求冪
為了計算數字的冪,Math 類別提供了一個 pow 方法,該方法具有以下簽名:static double pow(double a, double b)
此方法將參數“a”進行“b”次方。例子:
public static void main(String[] args) {
System.out.println(Math.pow(1,2)); // 1.0
System.out.println(Math.pow(2,2)); // 4.0
System.out.println(Math.pow(3,2)); // 9.0
System.out.println(Math.pow(4,2)); // 16.0
System.out.println(Math.pow(5,2)); // 25.0
System.out.println(Math.pow(1,3)); // 1.0
System.out.println(Math.pow(2,3)); // 8.0
System.out.println(Math.pow(3,3)); // 27.0
System.out.println(Math.pow(4,3)); // 64.0
System.out.println(Math.pow(5,3)); // 125.0
}
拔根
Math 類別提供了平方根和立方根的方法。以下方法負責此過程:- 靜態雙開方(雙a)
- 靜態雙 cbrt(雙 a)
public static void main(String[] args) {
System.out.println(Math.sqrt(4)); // 2.0
System.out.println(Math.sqrt(9)); // 3.0
System.out.println(Math.sqrt(16)); // 4.0
System.out.println(Math.cbrt(8)); // 2.0
System.out.println(Math.cbrt(27)); // 3.0
System.out.println(Math.cbrt(125)); // 5.0
}
隨機數生成
為了產生隨機數,Math 類別提供了 random 方法。此方法產生 0.0 到 1.0 範圍內的隨機正實數(雙精度)。方法簽名如下圖所示:public static double random()
讓我們來看看例子:
public static void main(String[] args) {
for (int i = 0; i < 5; i++) {
System.out.println(Math.random());
}
}
執行main方法後,控制台顯示如下:
0.37057465028778513
0.2516253742011597
0.9315649439611121
0.6346725713527239
0.7442959932755443
透過一些操作,您可以使用 Math 類別的 random 方法來取得特定範圍內的整數隨機數。以下是一個函數範例,該函數接受兩個參數 min 和 max,並傳回一個介於 min(含)和 max(含)範圍內的隨機整數:
static int randomInARange(int min, int max) {
return (int) (Math.random() * ((max - min) + 1)) + min;
}
讓我們來寫一個 Main 方法,在其中測試 randomInARange 方法:
public class MathExample {
public static void main(String[] args) {
// Карта, в которой мы будем хранить количество выпадений Howого-то числа
Map<Integer, Integer> map = new TreeMap<>();
// За 10000 операций
for (int i = 0; i < 10000; i++) {
// Сгенерируем рандомное число от -10 включительно до 10 включительно
final Integer randomNumber = randomInARange(-10, 10);
if (!map.containsKey(randomNumber)) {
// Если карта еще не содержит "выпавшего случайного числа"
// Положим его в карту с кол-вом выпадений = 1
map.put(randomNumber, 1);
} else {
// Иначе, увеличим количество выпадений данного числа на 1
map.put(randomNumber, map.get(randomNumber) + 1);
}
}
// Выведем на экран содержимое карты в формате ключ=[meaning]
for (Map.Entry<Integer, Integer> entry : map.entrySet()){
System.out.println(String.format("%d=[%d]", entry.getKey(), entry.getValue()));
}
}
static int randomInARange(int min, int max) {
return (int) (Math.random() * ((max - min) + 1)) + min;
}
}
運行 main 方法後,輸出可能如下所示:
-10=[482]
-9=[495]
-8=[472]
-7=[514]
-6=[457]
-5=[465]
-4=[486]
-3=[500]
-2=[490]
-1=[466]
0=[458]
1=[488]
2=[461]
3=[470]
4=[464]
5=[463]
6=[484]
7=[479]
8=[459]
9=[503]
10=[444]
Process finished with exit code 0
四捨五入
對於 Java 中的數字舍入,工具之一是 Math 類別的方法。更準確地說,圓形、天花板和地板方法:- 靜態長圓(雙A)
- 靜態 int round(float a)
- 靜態雙層(雙A)
- 靜態雙層天花(雙A)
public static void main(String[] args) {
System.out.println(Math.round(1.3)); // 1
System.out.println(Math.round(1.4)); // 1
System.out.println(Math.round(1.5)); // 2
System.out.println(Math.round(1.6)); // 2
System.out.println(Math.floor(1.3)); // 1.0
System.out.println(Math.floor(1.4)); // 1.0
System.out.println(Math.floor(1.5)); // 1.0
System.out.println(Math.floor(1.6)); // 1.0
System.out.println(Math.ceil(1.3)); // 2.0
System.out.println(Math.ceil(1.4)); // 2.0
System.out.println(Math.ceil(1.5)); // 2.0
System.out.println(Math.ceil(1.6)); // 2.0
}
結論
在這篇文章中,我們對數學課進行了粗淺的了解。我們研究瞭如何使用此類:- 計算值的模;
- 計算三角函數的值;
- 計算數字的冪;
- 提取平方根和立方根;
- 產生隨機數;
- 整數。
GO TO FULL VERSION