java.lang math 類

來源:互聯網
上載者:User

標籤:自己實現   數學運算   pow   接受   mat   三角函數   類型   操作   lang   

我們在編程的過程中,經常對一些數字進行數學操作,比如我們想要求絕對值或餘弦什麼的。那這些方法是否需要我們自己實現嗎?其實在 java.lang 裡的 Math 類Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。我們就來學習一下吧!

先認識一些方法吧:

方法 傳回值 功能描述
sin(double numvalue) double 計算角 numvalue 的正弦值
cos(double numvalue) double 計算角 numvalue 的餘弦值
acos(double numvalue) double 計算 numvalue 的反餘弦
asin(double numvalue) double 計算 numvalue 的反正弦
atan(double numvalue) double 計算 numvalue 的反正切
pow(double a, double b) double 計算 a 的 b 次方
sqrt(double numvalue) double 計算給定值的正平方根
abs(int numvalue) int 計算 int 類型值 numvalue 的絕對值,也接收 long、float 和 double 類型的參數
ceil(double numvalue) double 返回大於等於 numvalue 的最小整數值
floor(double numvalue) double 返回小於等於 numvalue 的最大整數值
max(int a, int b) int 返回 int 型 a 和 b 中的較大值,也接收 long、float 和 double 類型的參數
min(int a, int b) int 返回 a 和 b 中的較小值,也可接受 long、float 和 double 類型的參數
rint(double numvalue) double 返回最接近 numvalue 的整數值
round(T arg) arg 為 double 時返回 long,為 float 時返回 int 返回最接近arg的整數值
random() double 返回帶正號的 double 值,該值大於等於 0.0 且小於 1.0
上面都是一些常用的方法,如果同學們以後還會用到極座標、對數等,就去查一查手冊吧。

我們來看看部分方法的範例程式碼:

public class MathTest {
public static void main(String[] args) {
System.out.println(Math.abs(-12.7));
System.out.println(Math.ceil(12.7));
System.out.println(Math.rint(12.4));
System.out.println(Math.random());
}
}
輸入結果:

java.lang math 類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.