java中立方根方法_java基础:4、java中Math类的常用方法?
4、java中Math類的常用方法?
Java的Math類封裝了很多與數學有關的屬性和方法。如下所示:
System.out.println("計算平方根--Math.sqrt(81)--? "+Math.sqrt(81));//9.0
System.out.println("計算立方根--Math.cbrt(27)--? "+Math.cbrt(27));//3.0
System.out.println("計算x與y平方和的平方根--Math.sqrt(3,4)--? "+Math.hypot(3, 4));//5.0
System.out.println("計算a的b次方--Math.pow(4,2)--"+Math.pow(4,2));//16.0
System.out.println("計算e的x次方--Math.exp(3)--"+Math.exp(3));//20.085536923187668
System.out.println("求最大值--Math.max(81,56)--"+Math.max(81,56));//81
System.out.println("求最小值--Math.min(3.1,6.6)--"+Math.min(3.1,6.6));//3.1
System.out.println("求絕對值--Math.abs(90.4)--"+Math.abs(90.4));//90.4
System.out.println("求絕對值--Math.abs(-81.6)--"+Math.abs(-81.6));//81.6
//天花板
System.out.println("返回比它大的最小整數--Math.ceil(-10.1)--"+Math.ceil(-10.1));//-10.0
System.out.println("返回比它大的最小整數--Math.ceil(10.7)--"+Math.ceil(10.7));//11.0
System.out.println("返回比它大的最小整數--Math.ceil(-0.7)--"+Math.ceil(-0.7));//0.0
System.out.println("返回比它大的最小整數--Math.ceil(0.0)--"+Math.ceil(0.0));//0.0
//地板
System.out.println("返回比它小的最大整數--Math.floor(-10.1)--? "+Math.floor(-10.1));//-11.0
System.out.println("返回比它小的最大整數--Math.floor(10.7)--? "+Math.floor(10.7));//10.0
System.out.println("返回比它小的最大整數--Math.floor(-0.7)--? "+Math.floor(-0.7));//-1.0
System.out.println("返回比它小的最大整數--Math.floor(-0.0)--? "+Math.floor(-0.0));//-0.0
System.out.println("返回比它小的最大整數--Math.floor(-2.5)--? "+Math.floor(-2.5));//-3.0
//隨機數
System.out.println("[0,1)之間的隨機數--? "+Math.random());//0.48482777399357113
System.out.println("[0,100)之間的隨機數--? "+Math.random()*100);//92.73258074803167
//四舍五入,返回double值
System.out.println("返回最接近參數的整數,若兩個數同樣接近,返回偶數那個--Math.rint(-2.5)--? "+Math.rint(-2.5));//-2.0
System.out.println("返回最接近參數的整數,若兩個數同樣接近,返回偶數那個--Math.rint(-1.9)--? "+Math.rint(-1.9));//-2.0
System.out.println("返回最接近參數的整數,若兩個數同樣接近,返回偶數那個--Math.rint(2.5)--? "+Math.rint(2.5));//2.0
System.out.println("返回最接近參數的整數,若兩個數同樣接近,返回偶數那個--Math.rint(10.7)--? "+Math.rint(10.7));//11.0
System.out.println("返回最接近參數的整數,若兩個數同樣接近,返回偶數那個--Math.rint(-1.3)--? "+Math.rint(-1.3));//-1.0
//四舍五入,float返回int類型,double返回long類型
System.out.println("四舍五入--Math.round(-0.7)--? "+Math.round(-0.7));//-1
System.out.println("四舍五入--Math.round(-1.5)--? "+Math.round(-1.5));//-1
System.out.println("四舍五入--Math.round(-10.07)--? "+Math.round(-10.07));//-10
System.out.println("四舍五入--Math.round(-2.5)--? "+Math.round(-2.5));//-2
System.out.println("比a大一點點的浮點數--Math.nextUp(2.5)--? "+Math.nextUp(2.5));//2.5000000000000004
System.out.println("比a小一點點的浮點數--Math.nextDown(2.5)--? "+Math.nextDown(2.5));//2.4999999999999996
System.out.println("比a大或者小一點點的浮點數--Math.nextAfter(1.2,2.5)--? "+Math.nextAfter(1.2,2.5));//1.2000000000000002
System.out.println("比a大或者小一點點的浮點數--Math.nextAfter(1.2,-1)--? "+Math.nextAfter(1.2,-1));//1.1999999999999997
總結
以上是生活随笔為你收集整理的java中立方根方法_java基础:4、java中Math类的常用方法?的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: MC里的奇葩玩法我的世界的奇葩玩法
- 下一篇: 头条上发布哪些内容观众最喜欢和播放量最高