Math類提供了常用的一些數(shù)學(xué)函數(shù),如:三角函數(shù)、對數(shù)、指數(shù)等。一個數(shù)學(xué)公式如果想用代碼表示,則可以將其拆分然后套用Math類下的方法即可。
Math.abs(12.3); //12.3 返回這個數(shù)的絕對值 Math.abs(-12.3); //12.3 Math.copySign(1.23, -12.3); //-1.23,返回第一個參數(shù)的量值和第二個參數(shù)的符號 Math.copySign(-12.3, 1.23); //12.3 Math.signum(x); //如果x大于0則返回1.0,小于0則返回-1.0,等于0則返回0 Math.signum(12.3); //1.0 Math.signum(-12.3); //-1.0 Math.signum(0); //0.0 //指數(shù) Math.exp(x); //e的x次冪 Math.expm1(x); //e的x次冪 - 1 Math.scalb(x, y); //x*(2的y次冪) Math.scalb(12.3, 3); //12.3*23 //取整 Math.ceil(12.3); //返回最近的且大于這個數(shù)的整數(shù)13.0 Math.ceil(-12.3); //-12.0 Math.floor(12.3); //返回最近的且小于這個數(shù)的整數(shù)12.0 Math.floor(-12.3); //-13.0 //x和y平方和的二次方根 Math.hypot(x, y); //√(x2+y2) //返回概述的二次方根 Math.sqrt(x); //√(x) x的二次方根 Math.sqrt(9); //3.0 Math.sqrt(16); //4.0 //返回該數(shù)的立方根 Math.cbrt(27.0); //3 Math.cbrt(-125.0); //-5 //對數(shù)函數(shù) Math.log(e); //1 以e為底的對數(shù) Math.log10(100); //10 以10為底的對數(shù) Math.log1p(x); //Ln(x+ 1) //返回較大值和較小值 Math.max(x, y); //返回x、y中較大的那個數(shù) Math.min(x, y); //返回x、y中較小的那個數(shù) //返回 x的y次冪 Math.pow(x, y); Math.pow(2, 3); //即23 即返回:8 //隨機(jī)返回[0,1)之間的無符號double值 Math.random(); //返回最接近這個數(shù)的整數(shù),如果剛好居中,則取偶數(shù) Math.rint(12.3); //12.0 Math.rint(-12.3); //-12.0 Math.rint(78.9); //79.0 Math.rint(-78.9); //-79.0 Math.rint(34.5); //34.0 Math.rint(35.5); //36.0 Math.round(12.3); //與rint相似,返回值為long //三角函數(shù) Math.sin(α); //sin(α)的值 Math.cos(α); //cos(α)的值 Math.tan(α); //tan(α)的值 //求角 Math.asin(x/z); //返回角度值[-π/2,π/2] arc sin(x/z) Math.acos(y/z); //返回角度值[0~π] arc cos(y/z) Math.atan(y/x); //返回角度值[-π/2,π/2] Math.atan2(y-y0, x-x0); //同上,返回經(jīng)過點(diǎn)(x,y)與原點(diǎn)的的直線和經(jīng)過點(diǎn)(x0,y0)與原點(diǎn)的直線之間所成的夾角 Math.sinh(x); //雙曲正弦函數(shù)sinh(x)=(exp(x) - exp(-x)) / 2.0; Math.cosh(x); //雙曲余弦函數(shù)cosh(x)=(exp(x) + exp(-x)) / 2.0; Math.tanh(x); //tanh(x) = sinh(x) / cosh(x); //角度弧度互換 Math.toDegrees(angrad); //角度轉(zhuǎn)換成弧度,返回:angrad * 180d / PI Math.toRadians(angdeg); //弧度轉(zhuǎn)換成角度,返回:angdeg / 180d * PI
新聞熱點(diǎn)
疑難解答
圖片精選