JAVA Math類方法

1. abs()

abs() 返回參數的絕對值。參數可以是 int, float, long, double, short, byte類型。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		Integer a = -5;
        double d = -521;
        float f = -21;    
                        
        System.out.println(Math.abs(a));
        System.out.println(Math.abs(d));     
        System.out.println(Math.abs(f));    
		
	}
}

輸出如下:

5
521.0
21.0
2.ceil(),floor(),round(),rint()

ceil() 方法可對一個數進行上舍入,返回值大於或等於給定的參數,類型爲雙精度浮點型。floor() 方法可對一個數進行下舍入,返回給定參數最大的整數,該整數小於或等給定的參數。round() 方法返回一個最接近的 int、long 型值,round 表示"四捨五入",算法爲Math.floor(x+0.5) ,即將原來的數字加上 0.5 後再向下取整。rint() 方法返回最接近參數的整數值。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double a = 100.521;
		double b = -100.521;
        System.out.println(Math.ceil(a));
        System.out.println(Math.ceil(b));
        System.out.println(Math.floor(a));
        System.out.println(Math.floor(b));
        System.out.println(Math.round(a));
        System.out.println(Math.round(b)); 
        System.out.println(Math.rint(a));
        System.out.println(Math.rint(b)); 
	}
}

輸出如下:

101.0
-100.0
100.0
-101.0
101
-101
101.0
-101.0
3.min(),max()

min() 方法用於返回兩個參數中的最小值。max() 方法用於返回兩個參數中的最大值。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println(Math.max(52.1, 5.21));      
        System.out.println(Math.min(52.1, 5.21));  
	}
}

輸出如下:

52.1
5.21
4.exp(),log()

exp() 方法用於返回自然數底數e的參數次方。log() 方法用於返回參數的自然數底數的對數值。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 	double x = 2;
	        System.out.printf("%.4f%n", Math.E);
	        System.out.printf("%.3f%n",Math.exp(x));  
	        System.out.printf("%.3f%n",Math.log(x));
	}
}

輸出如下:

2.7183
7.389
0.693
5.pow(),sqrt()

pow() 方法用於返回第一個參數的第二個參數次方。sqrt() 方法用於返回參數的算術平方根。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 	double x = 2;double y=4;
	        System.out.printf(" %.3f%n",Math.pow(x, y));
	        System.out.printf(" %.3f%n",Math.sqrt(x));
	}
}

輸出如下:

 16.000
 1.414
6.toDegrees(),toRadians()

toDegrees() 方法用於將參數轉化爲角度。toRadians() 方法用於將角度轉換爲弧度。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double x = 90.0;
        double y = 180.0;
        System.out.println( Math.toRadians(x) );
        System.out.println( Math.toRadians(y) );
        System.out.println( Math.toDegrees(x) );
        System.out.println( Math.toDegrees(y) );
	}
}

輸出如下:

1.5707963267948966
3.141592653589793
5156.620156177409
10313.240312354817
7.sin(),cos(),tan(),asin(),acos(),atan()

sin() 方法用於返回指定double類型參數的正弦值。cos() 方法用於返回指定double類型參數的餘弦值。tan() 方法用於返回指定double類型參數的正切值。asin() 方法用於返回指定double類型參數的反正弦值。acos() 方法用於返回指定double類型參數的反餘弦值。atan() 方法用於返回指定double類型參數的反正切值。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double degrees = 45.0;
        double radians = Math.toRadians(degrees);
        System.out.format("%.4f%n",Math.PI);
        System.out.format("%.4f 的反正弦值爲 %.4f 度 %n", Math.sin(radians), Math.toDegrees(Math.asin(Math.sin(radians))));
        System.out.format("%.4f 的反餘弦值爲 %.4f 度 %n", Math.cos(radians), Math.toDegrees(Math.acos(Math.sin(radians))));
        System.out.format("%.4f 的反正切值 %.4f 度 %n", Math.cos(radians), Math.toDegrees(Math.atan(Math.sin(radians))));
	}
}

輸出如下:

3.1416
0.7071 的反正弦值爲 45.00000.7071 的反餘弦值爲 45.00000.7071 的反正切值 35.2644
8.atan2()

atan2() 方法用於將矩形座標 (x, y) 轉換成極座標 (r, theta),返回所得角 theta。該方法通過計算 y/x 的反正切值來計算相角 theta,範圍爲從 -pi 到 pi。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		double x = 0;
        double y = 5.21;
        System.out.println( Math.atan2(y, x) );
	}
}

輸出如下:

1.5707963267948966
9.random()

random() 方法用於返回一個隨機數,隨機數範圍爲 0.0 =< Math.random < 1.0。舉例如下:

public class NumberMath {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
        System.out.println( Math.random() );
	}
}

輸出如下:

0.18612266191439286
0.09064001133127308
0.5354137430772037
0.5061781207429481
0.051795530334382334
0.8652753270947399
0.9122117569410407
0.7102580911768844
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章