深入了解Java核心類庫--Math類
Java常用類庫Math
類Math包含用于執(zhí)行基本數(shù)字運(yùn)算的方法,例如基本指數(shù),對(duì)數(shù),平方根和三角函數(shù)
一、Field Summary
| Modifier and Type | Field | Description |
|---|---|---|
| static double | E | 自然對(duì)數(shù)的基數(shù) |
| static double | PI | π |
二、Method Summary
2.1 常用方法
| Modifier and Type | Field | Description |
|---|---|---|
| ① | ||
| static double | ceil(double a) | 返回≥a的最小整數(shù) |
| static double | floor(double a) | 返回≤a的最大整數(shù) |
| static int | round(float a) | 返回四舍五入后的值 |
| ② | ||
| static T | max(T a, T b) | 返回兩個(gè)數(shù)據(jù)類型為T中較大的 |
| static T | min(T a, T b) | 返回兩個(gè)數(shù)據(jù)類型為T中較x小的 |
| ③ | ||
| static double | random() | 返回[0.0,1.0)。 |
| static double | sqrt(double a) | 返回正平方根。 |
| static T | abs(T a) | 返回?cái)?shù)據(jù)類型為T的絕對(duì)值 |
| static double | pow(double a, double b) | 返回a^b |
| static double | log(double a) | 返回基數(shù)為e的對(duì)數(shù) |
| static double | log10(double a) | 返回基數(shù)為10的對(duì)數(shù) |
2.1.1 部分方法源碼
public static long round(double a) {
long longBits = Double.doubleToRawLongBits(a);
long biasedExp = (longBits & DoubleConsts.EXP_BIT_MASK)
>> (DoubleConsts.SIGNIFICAND_WIDTH - 1);
long shift = (DoubleConsts.SIGNIFICAND_WIDTH - 2
+ DoubleConsts.EXP_BIAS) - biasedExp;
if ((shift & -64) == 0) { // shift >= 0 && shift < 64
// a is a finite number such that pow(2,-64) <= ulp(a) < 1
long r = ((longBits & DoubleConsts.SIGNIF_BIT_MASK)
| (DoubleConsts.SIGNIF_BIT_MASK + 1));
if (longBits < 0) {
r = -r;
}
// In the comments below each Java expression evaluates to the value
// the corresponding mathematical expression:
// (r) evaluates to a / ulp(a)
// (r >> shift) evaluates to floor(a * 2)
// ((r >> shift) + 1) evaluates to floor((a + 1/2) * 2)
// (((r >> shift) + 1) >> 1) evaluates to floor(a + 1/2)
return ((r >> shift) + 1) >> 1;
} else {
// a is either
// - a finite number with abs(a) < exp(2,DoubleConsts.SIGNIFICAND_WIDTH-64) < 1/2
// - a finite number with ulp(a) >= 1 and hence a is a mathematical integer
// - an infinity or NaN
return (long) a;
}
}
public static int max(int a, int b) {
return (a >= b) ? a : b;
}
public static int min(int a, int b) {
return (a <= b) ? a : b;
}
public static int abs(int a) {
return (a < 0) ? -a : a;
}
2.1.2 具體實(shí)現(xiàn)
public class Test {
public static void main(String[] args) {
System.out.println("≥3.2的最小整數(shù)為:"+Math.ceil(3.2));//output:4
System.out.println("≤3.2的最大整數(shù)為:"+Math.floor(3.2));//output:3
System.out.println("3.2四舍五入為:"+Math.round(3.2));//output:3
System.out.println("-1,5中較大的數(shù)為:"+Math.max(-1,5));//output:5
System.out.println("-1,5中較小的數(shù)為:"+Math.min(-1,5));//output:-1
System.out.println("隨機(jī)產(chǎn)生[0,5)范圍的數(shù)"+Math.random()*5);//output:[0,5)中任意的隨機(jī)數(shù)
System.out.println("25的平方根為:"+Math.sqrt(25));//output:5
System.out.println("-9的絕對(duì)值為:"+Math.abs(-9));//output:9
System.out.println("2^3的值為:"+Math.pow(2,3));//output:8
System.out.println("以e為基數(shù)的對(duì)數(shù)為:"+Math.log(10));
System.out.println("以10為基數(shù)的對(duì)數(shù)為:"+Math.log10(100));//output:2
}
}
2.2 算數(shù)運(yùn)算
| Modifier and Type | Field | Description |
|---|---|---|
| static T | addExact(T x, T y) | 返回x+y,溢出則拋出異常T(int,long) |
| static T | multiplyExact(A x, B y) | 返回x*y,結(jié)果溢出則拋出異常int(int,int),long(long,int/long) |
| static long | multiplyFull(int x, int y) | 返回(long)x*(long)y |
| static T | floorDiv(A x, B y) | 返回≤ x/y的最大值,y=0則拋出ArithmeticException異常,int(int,int),long(long,int/long |
| static T | floorMod(A x, B y) | 返回floor(x%y),即x-(x/y)*y,int(int/long,int),long(long,long) |
2.3 三角函數(shù)
| Modifier and Type | Field | Description |
|---|---|---|
| static double | sin(double a) | 返回角度的三角正弦值 |
| static double | cos(double a) | 返回角度的三角余弦值 |
| static double | tan(double a) | 返回角度的三角正切 |
| static double | asin(double a) | 返回a的反正弦值,返回的角度-pi/2~pi/2 |
| static double | acos(double a) | 返回a的反余弦值,返回的角度0.0~pi |
| static double | atan(double a) | 返回a的反正切值,返回的角度-pi/2~pi/2 |
2.4 其他不常用方法
| Modifier and Type | Field | Description |
|---|---|---|
| static double | cosh(double x) | 返回 double值的雙曲余弦值 |
| static double | cbrt(double a) | 返回 double值的多維數(shù)據(jù)集根 |
| static double | copySign(double magnitude, double sign) | 返回帶有第二個(gè)浮點(diǎn)參數(shù)符號(hào)的第一個(gè)浮點(diǎn)參數(shù) |
| static float | copySign(float magnitude, float sign) | 返回帶有第二個(gè)浮點(diǎn)參數(shù)符號(hào)的第一個(gè)浮點(diǎn)參數(shù) |
| static int | decrementExact(int a) | 返回a-1,如果結(jié)果溢出int則拋出異常 |
| static long | decrementExact(long a) | 返回a-1,如果結(jié)果溢出long則拋出異常 |
| static double | exp(double a) | 返回e^a |
| static double | expm1(double x) | 返回 e^x - 1 |
| static double | fma(double a, double b, double c) | 返回a*b+c |
| static float | fma(float a, float b, float c) | 返回a*b+c |
| static int | getExponent(double d) | 返回 double表示中使用的無偏指數(shù) |
| static int | getExponent(float f) | 返回 float表示中使用的無偏指數(shù) |
| static double | hypot(double x, double y) | 返回sqrt( x 2 + y 2 ),沒有中間溢出或下溢 |
| static double | IEEEremainder(double f1, double f2) | 根據(jù)IEEE 754標(biāo)準(zhǔn)規(guī)定,計(jì)算兩個(gè)參數(shù)的余數(shù)運(yùn)算 |
| static int | incrementExact(int a) | 返回以1遞增的參數(shù),如果結(jié)果溢出 int則拋出異常 |
| static long | incrementExact(long a) | 返回以1遞增的參數(shù),如果結(jié)果溢出 long則拋出異常 |
| static double | log1p(double x) | 返回參數(shù)和的總和的自然對(duì)數(shù) |
| static long | multiplyHigh(long x, long y) | 返回 long作為兩個(gè)64位因子的128位乘積的最高64位 |
| static int | negateExact(int a) | 返回參數(shù)的否定,如果結(jié)果溢出 int則拋出異常 |
| static long | negateExact(long a) | 返回參數(shù)的否定,如果結(jié)果溢出 long則拋出異常 |
| static double | nextAfter(double start, double direction) | 返回第二個(gè)參數(shù)方向上第一個(gè)參數(shù)旁邊的浮點(diǎn)數(shù) |
| static float | nextAfter(float start, double direction) | 返回第二個(gè)參數(shù)方向上第一個(gè)參數(shù)旁邊的浮點(diǎn)數(shù) |
| static double | nextDown(double d) | 返回負(fù)無窮大方向上與 d相鄰的浮點(diǎn)值 |
| static float | nextDown(float f) | 返回負(fù)無窮大方向上與 f相鄰的浮點(diǎn)值 |
| static double | nextUp(double d) | 返回正無窮大方向上與 d相鄰的浮點(diǎn)值 |
| static float | nextUp(float f) | 返回正無窮大方向上與 f相鄰的浮點(diǎn)值 |
| static double | rint(double a) | 返回與 double值最接近的 double值,該值等于數(shù)學(xué)整數(shù) |
| static double | scalb(double d, int scaleFactor) | 返回 d ×2 scaleFactor舍入,就像通過單個(gè)正確舍入的浮點(diǎn)乘以雙 scaleFactor值集的成員一樣 |
| static float | scalb(float f, int scaleFactor) | 返回 f ×2 scaleFactor舍入,就像通過單個(gè)正確舍入的浮點(diǎn)乘以浮點(diǎn)值集的成員一樣 |
| static double | signum(double d) | 返回參數(shù)的signum函數(shù); 如果參數(shù)為零,則為零;如果參數(shù)大于零,則為1.0;如果參數(shù)小于零,則為-1.0 |
| static float | signum(float f) | 返回參數(shù)的signum函數(shù); 如果參數(shù)為零則為零,如果參數(shù)大于零則為1.0f,如果參數(shù)小于零則為-1.0f |
| static double | sinh(double x) | 返回 double值的雙曲正弦值 |
| static int | subtractExact(int x, int y) | 返回參數(shù)的差異,如果結(jié)果溢出 int則拋出異常 |
| static long | subtractExact(long x, long y) | 返回參數(shù)的差異,如果結(jié)果溢出 long則拋出異常 |
| static double | tanh(double x) | 返回 double值的雙曲正切值 |
| static double | toDegrees(double angrad) | 將以弧度測量的角度轉(zhuǎn)換為以度為單位測量的近似等效角度 |
| static int | toIntExact(long value) | 返回long參數(shù)的值; 如果值溢出int則拋出異常 |
| static double | toRadians(double angdeg) | 將以度為單位測量的角度轉(zhuǎn)換為以弧度為單位測量的近似等效角度 |
| static double | ulp(double d) | 返回參數(shù)的ulp大小 |
| static float | ulp(float f) | 返回參數(shù)的ulp大小 |
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java8實(shí)現(xiàn)任意參數(shù)的鏈棧
這篇文章主要為大家詳細(xì)介紹了Java8實(shí)現(xiàn)任意參數(shù)的鏈棧,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-10-10
SpringBoot中使用spring-retry 解決失敗重試調(diào)用
本文主要介紹了SpringBoot中使用spring-retry 解決失敗重試調(diào)用,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-07-07
Java實(shí)現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式
這篇文章主要介紹了Java實(shí)現(xiàn)將圖片上傳到webapp路徑下 路徑獲取方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-11-11
Java的Struts2框架中攔截器使用的實(shí)例教程
攔截器是Struts框架的重要特性,Struts中每一個(gè)Action請(qǐng)求都包裝在一系列的攔截器的內(nèi)部,這里我們就來看一下Java的Struts2框架中攔截器使用的實(shí)例教程2016-07-07
java虛擬機(jī)參數(shù)-D、-X和-XX的區(qū)別小結(jié)
本文主要介紹了java虛擬機(jī)參數(shù)-D、-X和-XX的區(qū)別小結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-06-06
防止未登錄用戶操作—基于struts2攔截器的簡單實(shí)現(xiàn)
下面小編就為大家?guī)硪黄乐刮吹卿浻脩舨僮鳌趕truts2攔截器的簡單實(shí)現(xiàn)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-10-10

