Java的Math工具類總結
一、常量
Math類中有兩個基本常量:自然對數e和圓周率π
二、三角函數相關
三、弧度與角度相關
四、乘方開方相關
五、四捨五入與符號相關
六、與計算機數據最小粒度相關
七、其他
八、附代碼。
double e = Math.E;//自然對數:2.718281828459045
System.out.println(e);
double pi = Math.PI;//圓周率:3.141592653589793
System.out.println(pi);
//返回一個弧度值的正弦
System.out.println(Math.sin(pi/2));//1.0
//返回一個弧度值的餘弦
System.out.println(Math.cos(3.14));//-0.9999987317275395
Advertisements
//返回一個弧度值的正切
System.out.println(Math.tan(pi/4));//0.9999999999999999
//返回一個數的反正弦(單位為弧度),參數範圍[-1,1],結果[-π/2,π/2];參數在範圍之外返回NaN
System.out.println(Math.asin(-1));//-1.5707963267948966【-π/2】
System.out.println(Math.asin(0)); //0.0
System.out.println(Math.asin(1)); //1.5707963267948966【π/2】
//返回一個數的反餘弦(單位為弧度),參數大小[-1,1],結果[0,π];參數在範圍之外返回NaN
Advertisements
System.out.println(Math.acos(pi));//NaN
System.out.println(Math.acos(-1));//3.141592653589793【π】
System.out.println(Math.acos(0)); //1.5707963267948966【π/2】
System.out.println(Math.acos(1)); //0.0
//返回一個數的反正切(單位為弧度),參數無大小限制,結果(-π/2,π/2);參數絕對值越大,結果的絕對值越接近π/2
System.out.println(Math.atan(5.99)); //1.405376940128626
System.out.println(Math.atan(0)); //0.0
System.out.println(Math.atan(-5.99));//-1.405376940128626
//返回雙曲餘弦:結果為(e^x+e^(-x))/2;值>=1
System.out.println(Math.cosh(3.14)); //11.573574828312076
//返回雙曲正弦:結果為(e^x-e^(-x))/2
System.out.println(Math.sinh(1)); //1.1752011936438014
//返回雙曲正切:結果為(e^x - e^-x)/(e^x + e^-x)
System.out.println(Math.tanh(1.1));//0.8004990217606297
//返回點(y,x)到原點連線與正X軸之間對應的逆時針角度對應的弧度。返回(-π,π]。
System.out.println(Math.atan2(1,1)); //0.7853981633974483【π/4】即45°
System.out.println(Math.atan2(-1,1)); //-0.7853981633974483【-π/4】即-45°
System.out.println(Math.atan2(0,1)); //0.0
System.out.println(Math.atan2(0,-1)); //3.141592653589793【π】180°
System.out.println(Math.atan2(1,-1)); //2.356194490192345【3π/4】135°
System.out.println(Math.atan2(1,0)); //1.5707963267948966【π/2】即90°
//將弧度值轉換成角度值
System.out.println(Math.toDegrees(pi/2));//90.0
//將角度值轉換成弧度值
System.out.println(Math.toRadians(90.0));//1.5707963267948966【π/2】
//返回一個正數的平方根
System.out.println(Math.sqrt(1234));//35.12833614050059
System.out.println(Math.sqrt(-1234));//NaN
//返回一個數的立方根
System.out.println(Math.cbrt(1000.7));//10.00233278910052
//返回一個數的e次冪
System.out.println(Math.exp(1)); //2.718281828459045
//返回一個數的e次冪-1
System.out.println(Math.expm1(1));//1.718281828459045
System.out.println(Math.expm1(-0));//0.0
//滿足2^n <= x的最大n值,n為整數
System.out.println(Math.getExponent(213.345)); //7
System.out.println(Math.getExponent(32)); //5
System.out.println(Math.getExponent(31.9999)); //4
//返回(a^2+b^2)^0.5的值
System.out.println(Math.hypot(1, 1)); //1.4142135623730951
//返回e^x = a中x的值,其中a為參數
System.out.println(Math.log(e)); //1.0
System.out.println(Math.log(e*e)); //2.0
//返回10^x = a中x的值,其中a為參數
System.out.println(Math.log10(100)); //2.0
//返回10^x - 1 = a 中x的值,其中a為參數
System.out.println(Math.log1p(e*e-1));//2.0
//返回a^b
System.out.println(Math.pow(11.09, 3)); //1363.938029
//返回a*2^b
System.out.println(Math.scalb(3, 4));//48.0
//返回一個大於等於該參數的整數
System.out.println(Math.ceil(1000.7));//1001.0
System.out.println(Math.ceil(-1000.7));//-1000
//將第二個參數的符號賦值給第一個參數后返回最終的第一個參數
System.out.println(Math.copySign(0.2, 1.55));//0.2
System.out.println(Math.copySign(0.2, -1.55));//-0.2
System.out.println(Math.copySign(-0.2, 8789));//0.2
//返回一個數的絕對值,參數可以為double、float、int和long
System.out.println(Math.abs(-0.0));//0.0
//返回最接近的整數值,double類型
System.out.println(Math.rint(9.908745));//10.0
System.out.println(Math.rint(-9.408745));//-9.0
//對參數值四捨五入,保留整數,返回這個long類型的整數
System.out.println(Math.round(9.908745));//10
System.out.println(Math.round(9.408745));//9
System.out.println(Math.round(-9.908745));//-10
System.out.println(Math.round(-9.408745));//-9
//返回小於等於這個參數的整數值
System.out.println(Math.floor(2.560)); //2.0
System.out.println(Math.floor(-2.560)); //-3.0
//判斷參數的正負,返回1.0則表示是正數,0.0表示0,-1.0表示負數
System.out.println(Math.signum(156.98));//1.0
System.out.println(Math.signum(0.00)); //0.0
System.out.println(Math.signum(-15.98));//-1.0
//nextAfter(a,b):如果b>a則返回比a大一點的浮點數,如果b=a就返回a的浮點數,如果b<a則返回比a小一點的浮點數
//例如2.0和3.0之間的數,在數學中是無限的,但是在計算機中是有限的(因為沒有無限內存)。
System.out.println(Math.nextAfter(12123.00, 12123.0)); //12123.0
System.out.println(Math.nextAfter(12123.00, 12123.001)); //12123.000000000002
System.out.println(Math.nextAfter(12123.00, 12122.999)); //12122.999999999998
//返回比參數大的第一個浮點數
System.out.println(Math.nextUp(12123.00));//12123.000000000002
//返回一個數字和距其最近的數字之間的距離,參考nextAfter
System.out.println(Math.ulp(2.0)); //4.440892098500626E-16
//返回一指定數字被另一指定數字相除的絕對值最小的餘數。
System.out.println(Math.IEEEremainder(1, 1));//0.0
System.out.println(Math.IEEEremainder(56, 9));//2.0
System.out.println(Math.IEEEremainder(56, 10));//-4.0
System.out.println(Math.IEEEremainder(560.25, 48));//-15.75
System.out.println(Math.IEEEremainder(-560.25, 48));//15.75
System.out.println(Math.IEEEremainder(560.25, -48));//-15.75
//返回兩個數中的最大或最小數
System.out.println(Math.max(55.21, 55.20));//55.21
System.out.println(Math.min(55.21, 55.20));//55.2
//返回[0.0,1)之間的一個隨機數
System.out.println(Math.random()); //隨機數:0.06456256840265118
System.out.println(Math.random()*100); //[0.0,100)之間的隨機數:98.23100954931193