java中常見的math方法

來源:互聯網
上載者:User

標籤:輸出   rac   str   其他   pac   one   絕對值   down   擷取   

java.lang.Math :

 絕對值:static int abs(int a) static long abs(long a) static float abs(float a) static double abs(double a)  極值:static int max(int a, int b) static long max(long a, long b) static float max(float a, float b) static double max(double a, double b) static int min(int a, int b) static long min(long a, long b) static float min(float a, float b) static double min(double a, double b)  三角:static double sin(double a)          //正弦函數static double sinh(double x)        //雙曲正弦函數 static double cos(double a)         //餘弦函數static double cosh(double x)       //雙曲餘弦函數 static double tan(double a)         //正切函數static double tanh(double x)       //雙曲正切函數 static double asin(double a)       static double acos(double a) static double atan(double a) static double atan2(double y, double x)  對數,指數:static double log(double a) static double log10(double a) static double log1p(double x)             // ln (x+1) static double exp(double a)                static double expm1(double x)           // e^a - 1 冪,根:static double pow(double a, double b) static double sqrt(double a) static double cbrt(double a)                              // cube rootstatic double hypot(double x, double y)           // sqrt(x2 +y2)static double scalb(double d, int scaleFactor)   // d × 2scaleFactorstatic float scalb(float f, int scaleFactor)            // f × 2scaleFactor 隨機:static double random()  最接近的整數:static long round(double a) static int round(float a)  浮點數:static double nextAfter(double start, double direction) static float nextAfter(float start, double direction) static double nextDown(double d) static float nextDown(float f) static double nextUp(double d) static float nextUp(float f) static double ceil(double a) static double floor(double a) static int floorDiv(int x, int y) static long floorDiv(long x, long y) static int floorMod(int x, int y) static long floorMod(long x, long y)   無溢出計算:如果溢出則拋異常static int toIntExact(long value) static int addExact(int x, int y) static long addExact(long x, long y) static int subtractExact(int x, int y) static long subtractExact(long x, long y) static int multiplyExact(int x, int y) static long multiplyExact(long x, long y) static int negateExact(int a) static long negateExact(long a) static int incrementExact(int a) static long incrementExact(long a) static int decrementExact(int a) static long decrementExact(long a)  符號:static double signum(double d) static float signum(float f)  角度轉換:static double toDegrees(double angrad) static double toRadians(double angdeg)  其他:static int getExponent(double d) static int getExponent(float f)  static double rint(double a)                 // round int static double copySign(double magnitude, double sign)        //採用 magnitude 的值和 sign 的符號static float copySign(float magnitude, float sign)  static double IEEEremainder(double f1, double f2)              // f1 除以 f2的餘數 static double ulp(double d) static float ulp(float f)   
 1 package com.hone.test; 2  3 /** 4  * 測試java.lang.Math的方法 5  * @author Xia 6  * 7  */ 8 public class Maths { 9 10     public static void main(String[] args) {11         double d = 123.456;12         double d2 = -123.456;13         14         int i = 123;15         int b = 389;16         System.out.printf("%.2f%n", d2);                //按照格式輸出2位小數17         System.out.printf("%.3f%n", Math.abs(d2));        //Math.abs(d2) 擷取絕對值18         19         //------------沒有溢出計算,如果有溢出則拋出異常--------------------20         System.out.println(Math.addExact(i, b));21         System.out.println(Math.incrementExact(i));        //如果溢出則加1   ------12422         23         //------------極值--------------------24         System.out.println(Math.max(d, d2));            //選擇更大的一個值      123.45625         System.out.println(Math.min(i, b));            //選擇更小的一個值          12326         27         //------------對數,指數--------------------28         System.out.println(Math.log(d));                //輸出以e為底的對數29         System.out.println(Math.log10(100.0));             //輸出以10為底的對數30         System.out.println(Math.log10(100.0));             //輸出以10為底的對數31         32         System.out.println(Math.exp(1));                 //輸出以e為底的指數33         34         //------------冪,根:--------------------35         System.out.println(Math.pow(d, d2));            //表示d^d236         System.out.println(Math.sqrt(d));                 //取d的均方根37         System.out.println(Math.cbrt(1000));             //立方根38         39         //------------隨機數--------------------40         System.out.println(Math.random());                //取(0,1)之間的隨機數41         42         //------------最接近的整數:--------------------43         System.out.println(Math.round(d));                //四捨五入取整44         45         System.out.println(Math.ceil(d));                 //向上取整46         System.out.println(Math.floor(d));                 //向下取整47     }48 }

 

      

java中常見的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.