Math類常用方法(Java)


三角函數:
public static double sin (double radians)
public static double cos(double radians)
public static double tan(double radians)
public static double toRadians (double degree)
public static double toDegrees (double radians)
public static double asin (double a)
public static double acos (double b)
public static double atan (double a)
【sin、cos 和 tan 的參數都是以弧度為單位的角。asin、acos 和 atan 的返回值是在 -π/2 到 π/2 之間的一個弧度值。1度相當於 π/180 弧度,90 弧度相當於 π/2 弧度】例如:
Math.toDegrees ( Math.PI/2 )    returns  90.0
Math.toRadians ( 30 )    returns  π/6
Math.sin ( 0 )    returns  0.0
Math.sin ( Math.toRadians(270) )    returns -1.0
Math.sin ( Math.PI/6 )    returns  0.5
Math.sin ( Math.PI/2 )    returns  1.0
Math.cos ( 0 )    returns 1.0
Math.cos ( Math.PI/6 )    returns    0.866
Math.cos ( Math.PI/2 )    returns    0
Math.asin ( 0.5 )    returns   π/6

指數函數:
public static double exp ( double x )   【e^x】
public static double log ( double x )    【 ln x 】
public static double log10 (double x )    【 log10 (x) 】
public static double pow ( double a, double b )
public static double sqrt ( double x ) 
例如:
Math.exp ( 1 )    returns 2.71828
Math.log ( Math.E )    returns 1.0
Math.log10 ( 10 )    returns 1.0
Math.pow ( 2, 3 )    returns 8.0
Math.pow ( 3, 2 )    returns 9.0
Math.pow (3.5, 2.5 )    returns 22.91765
Math.sqrt ( 4 )    returns 2.0
Math.sqrt ( 10.5 )    returns 3.24

取整方法:
public static double ceil ( double x )  【向上取整】
public static double floor ( double x )    【向下取整】
public static double rint ( double x )    【取最接近的整數,如果有兩個同樣接近的整數(.5),就兩個中隨機取一個 】
public static int round ( float x )    /* Return (int)Math.floor (x + 0.5 ) */
public static long round ( double x )     /*  Return (long)Math.floor ( x + 0.5) */
例如:
Math.ceil ( 2.1 )      returns 3.0
Math.ceil ( 2.0 )      returns 2.0
Math.ceil ( -2.0 )     returns -2.0
Math.ceil ( -2.1 )     returns -2.0
Math.floor ( 2.1 )    returns 2.0
Math.floor ( 2.0 )    returns 2.0
Math.floor ( -2.0 )   returns -2.0
Math.floor ( -2.1 )   returns -3.0
Math.rint ( 2.1 )       returns 2.0
Math.rint ( -2.0 )      returns -2.0
Math.rint ( -2.1 )      returns -2.0
Math.rint ( 2.5 )       returns 2.0
Math.rint ( 3.5 )       returns 4.0
Math.rint ( -2.5 )     returns -2.0
Math.round ( 2.6f ) returns 3  // returns  int
Math.round ( 2.0 )  returns 2  // returns  long
Math.round ( -2.0f ) returns -2  
Math.round ( -2.6 ) returns -3


重載方法 abs 返回一個數(int、 long、 float、 double)的絕對值,如:
Math.abs ( -2.1 )   returns 2.1 ; Math 還有 max 和 min 方法,對比兩個數

因為 0 <= Math.random( ) < 1.0 , 若需 0 ~ 50 個隨機數,則是 ( int )( Math.random( )*(50 + 1))   
【記得 + 1】 ,隨機小寫字母是 ( char )( 'a' + Math.random( )*('z' - 'a' + 1) )





免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM