Java Math類的一些方法

來源:互聯網
上載者:User
/*java.lang包中的Math類提供有常量 * 並包含有用於執行基本數學運算的 * 方法,如初等指數、對數、平方根 * 用於進行更進階的數學運算。由於 * 在Math類的方法都是靜態,因此可 * 直接通過類來調用 * Math.cos(angle); * (使用靜態匯入(static import)的語言特性"import static java.lang.Math.*" * 就不必在每一個數學函數前面都寫Math了。這允許通過簡單的 * 名稱調用Math類中的方法,例如:“cos(sngle)”! *  */public class hello {public static void main(String[] args){/*1.常量和基本方法 * Math包含兩種常量 * 1>Math.E:代表自然對數的基數,double類型 * 2>Math.PI:圓周率,double類型 * Math類包含超過40個靜態方法 */double a=-191.635;double b=41.94;int c=27,d=65;System.out.printf("%.3f的絕對值是:%.3f%n",a,Math.abs(a));System.out.printf("比%.2f大的最小整數是%.0f%n",b,Math.ceil(b));System.out.printf("比%.2f小的最大整數是%.0f%n",b,Math.floor(b));/*指數和對數方法 * exp 自然對數的e次冪 * log 參數的自然對數 * pow第一個參數的第二個參數的次冪 * sprt返回參數的平方根 */double x=12.715;double y=3.96;System.out.printf("自然地數的基數是:%.4f%n", Math.E);System.out.printf("exp(%.3f)是%.3f%n",x,Math.exp(x));System.out.printf("log(%.3f)是%.3f%n",x,Math.log(x));System.out.printf("sqrt(%.3f)是%.3f%n",x,Math.sqrt(x));/* * 隨機數 * Math類的random()靜態方法返回在0.0和1.0之間的一個隨機數 * 區間包括0.0但不包括1.0 * 假如要產生一個0-9的整數 * 可以這樣int number=(int)(Math.random()*10) */System.out.print((int)(Math.random()*10));}}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.