一、基本常用的Math類方法
Math.abs( ) - 返回參數的絕對值。
參數可以是 int, float, long, double, short, byte類型
1 public static void main(String args[]){ 2 Integer a = -8; 3 double d = -100; 4 float f = -90; 5 6 System.out.println(Math.abs(a)); //8 7 System.out.println(Math.abs(d)); //100.0 8 System.out.println(Math.abs(f)); //90.0 9 }
Math.max( $ , $ )、Math.min( $ , $ ) - 返回兩個數的最大值、最小值
參數可以是 int, float, long, double類型(參數也可以是short,byte,但是返回類型會轉成int)
1 public static void main(String[] args) { 2 3 double a = 1; 4 double b = 2; 5 System.out.println(Math.max(a,b)); //2.0 6 System.out.println(Math.min(a,b)); //1.0 7 }
java Math.pow( $ , $ ) - 計算次方
java Math.pow()方法用於返回第一個參數的第二個參數次方。
public static void main(String args[]){ double x = 11.635; double y = 2.76; System.out.printf("e 的值為 %.4f%n", Math.E); System.out.printf("pow(%.3f, %.3f) 為 %.3f%n", x, y, Math.pow(x, y)); }
/*輸出結果: e 的值為 2.7183 pow(11.635, 2.760) 為 874.008 */
java Math.cbrt( ) - 開立方、Math.sqrt( )- 開平方
返回double類型
System.out.println(Math.sqrt(4.0));//輸出2.0 System.out.println(Math.cbrt(8.0));//輸出2.0
Java Math.log( ) 計算對數
Math.log() 方法用於返回參數的自然數底數的對數值。
1 public static void main(String args[]){ 2 double x =Math.E;
3 double y = 2.76;
5 System.out.printf("e 的值為 %.4f%n", Math.E);
6 System.out.printf("log(%.3f) 為 %.3f%n", x, Math.log(x)); 7 }
輸出結果為:
e 的值為 2.7183
log(e) 為 1.000 //log(e)=lne=1.0
Java Math.round( ) - 四舍五入
Java Math.round() 方法返回一個最接近的int、long型值。
1 public static void main(String args[]){ 2 double d = 100.675; 3 double e = 100.500; 4 float f = 100; 5 float g = 90f; 6 7 System.out.println(Math.round(d)); //101 8 System.out.println(Math.round(e)); //101 9 System.out.println(Math.round(f)); //100 10 System.out.println(Math.round(g)); //90 11 }
Java Math.rint( ) - 就近取整
返回與參數最接近的整數。返回類型為double。
1 public static void main(String args[]){ 2 double d = 10.675; 3 double e = 10.500; 4 float f = 100; 5 float g = 9.45; 6 7 System.out.println(Math.round(d)); //11.0 8 System.out.println(Math.round(e)); //11.0 9 System.out.println(Math.round(f)); //100.0 10 System.out.println(Math.round(g)); //9.0 }
Java Math.floor( ) - 向下取整
Math.floor() 方法可對一個數進行下舍入,直接取整,返回給定參數最大的整數,該整數小於或等給定的參數。參數可以使double和float ,返回double floor:地板
ava Math.ceil( ) - 向上取整
Math.ceil() 就是向上取整,參數可以使double和float ,返回double。ceil:天花板
1 public static void main(String args[]){ 2 double d = 100.675; 3 float f = -90; 4 5 System.out.println(Math.floor(d));//100.0 6 System.out.println(Math.floor(f));//-90.0 7 8 System.out.println(Math.ceil(d));//101.0 9 System.out.println(Math.ceil(f));//-90.0 10 }
Java Math.random( ) - 獲取隨機數
Java Math.random() 方法用於返回一個隨機數,隨機數范圍為 0.0 =< Math.random < 1.0。返回double類型
1 import java.util.Random; 2 3 public class RandomTest{ 4 public static void main(String[] args){ 5 Random rand=new Random(); 6 int i=(int)(Math.random()*100); // 生成0-100的隨機數 7 int j=rand.nextInt(100); // 這里是一個方法的重載,參數的內容是指定范圍 8 System.out.println("i:"+i+"\nj:"+j); // 分別輸出兩個隨機數 9 } 10 }
二、使用次數比較少的Math類函數
Java Math.toDegrees( ) - 弧度轉換成角度
Java中Math.toDegrees( )用於將以弧度測量的角度轉換為以度為單位的近似等效角度,也就是將-π/2到π/2之間的弧度值轉化為度。如果要將角度轉成成弧度用Math.toRadians。返回以度為單位的角度
public class MathtoDegreesDemo{ public static void main(String args[]){ System.out.println(Math.toDegrees(Math.PI/2)); } } //輸出:90.0
Java Math.toRadians( ) - 角度轉換為弧度
java中Math.toRadians( )用於將以度為單位的角度轉換為以弧度測量的大致相等的角度,也就是說將度轉化為-π/2到π/2之間的弧度值。如果要將弧度轉成成角度用Math.toDegrees。
public class MathtoDegreesDemo{ public static void main(String args[]){ System.out.println(Math.toRadians(30)); } } //輸出:π/6
計算三角函數
Java中Math.cos()用於計算余弦,返回的是 -1.0 到 1.0 之間的數,如果要想將余弦的值轉成角度可以用Math.acos()。
Java中Math.acos()用於計算反余弦,返回值的單位為弧度,對於[-1,1]之間的元素,函數值域為[0,pi],如果要想將角度轉成余弦的值可以Math.cos()。
Java中Math.sin()用於計算正弦,返回的是 -1.0 到 1.0 之間的數,如果要想將正弦轉換成角度可以用Math.asin()。
Java中Math.asin()用於計算反正弦,返回值的單位為弧度,返回的角度范圍在 -pi/2 到 pi/2 之間。如果要將角度轉換成正玄用Math.sin().
Math.tan()用於計算正切,Math.tanh() 用於計算雙曲正切。
java中Math.atan()用於計算反正切,返回的角度范圍在 -pi/2 到 pi/2 之間,如果要計算正切值用Math.tan()。
文章參考:http://www.51gjie.com/java/185.html & 菜鳥教程