深入了解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) | 將以弧度測(cè)量的角度轉(zhuǎn)換為以度為單位測(cè)量的近似等效角度 |
static int | toIntExact(long value) | 返回long參數(shù)的值; 如果值溢出int則拋出異常 |
static double | toRadians(double angdeg) | 將以度為單位測(cè)量的角度轉(zhuǎn)換為以弧度為單位測(cè)量的近似等效角度 |
static double | ulp(double d) | 返回參數(shù)的ulp大小 |
static float | ulp(float f) | 返回參數(shù)的ulp大小 |
總結(jié)
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關(guān)注腳本之家的更多內(nèi)容!
相關(guān)文章
Java實(shí)戰(zhàn)之郵件的撰寫和發(fā)送
這篇文章主要為大家詳細(xì)介紹了通過Java代碼實(shí)現(xiàn)郵件的撰寫和發(fā)送功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,需要的小伙伴們可以學(xué)習(xí)一下2021-11-11Java 不使用第三方變量交換兩個(gè)變量值的四種方法詳解
這篇文章主要介紹了四種不使用第三方變量交換兩個(gè)變量值的方法。文中對(duì)于四種方法進(jìn)行了詳細(xì)的分析,需要的小伙伴們可以跟隨小編一起學(xué)習(xí)一下2021-12-12淺談java二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換
下面小編就為大家?guī)硪黄獪\談二進(jìn)制、十進(jìn)制、十六進(jìn)制、字符串之間的相互轉(zhuǎn)換。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考,一起跟隨小編過來看看吧2016-06-06Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解
這篇文章主要介紹了Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解,讀寫鎖:一個(gè)資源能夠被多個(gè)讀線程訪問,或者被一個(gè)寫線程訪問但是不能同時(shí)存在讀寫線程,需要的朋友可以參考下2024-01-01Springboot 使用 maven-resources-plugin 打包變量替換ja
這篇文章主要介紹了Springboot 使用 maven-resources-plugin 打包變量替換jar沒有打包進(jìn)去、Jar包沒有被使用的解決方法,本文給大家介紹的非常詳細(xì),感興趣的朋友一起看看吧2024-08-08