深入了解Java核心類庫--Math類
更新時間:2021年07月29日 15:05:53 作者:入錯行的北北
本文是小編最新給大家整理的關于Java中Math類常用方法的知識,通過實例代碼給大家介紹的非常詳細,感興趣的朋友一起看看吧,
Java常用類庫Math
類Math包含用于執(zhí)行基本數(shù)字運算的方法,例如基本指數(shù),對數(shù),平方根和三角函數(shù)
一、Field Summary
Modifier and Type | Field | Description |
---|---|---|
static double | E | 自然對數(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) | 返回兩個數(shù)據(jù)類型為T中較大的 |
static T | min(T a, T b) | 返回兩個數(shù)據(jù)類型為T中較x小的 |
③ | ||
static double | random() | 返回[0.0,1.0)。 |
static double | sqrt(double a) | 返回正平方根。 |
static T | abs(T a) | 返回數(shù)據(jù)類型為T的絕對值 |
static double | pow(double a, double b) | 返回a^b |
static double | log(double a) | 返回基數(shù)為e的對數(shù) |
static double | log10(double a) | 返回基數(shù)為10的對數(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 具體實現(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("隨機產(chǎn)生[0,5)范圍的數(shù)"+Math.random()*5);//output:[0,5)中任意的隨機數(shù) System.out.println("25的平方根為:"+Math.sqrt(25));//output:5 System.out.println("-9的絕對值為:"+Math.abs(-9));//output:9 System.out.println("2^3的值為:"+Math.pow(2,3));//output:8 System.out.println("以e為基數(shù)的對數(shù)為:"+Math.log(10)); System.out.println("以10為基數(shù)的對數(shù)為:"+Math.log10(100));//output:2 } }
2.2 算數(shù)運算
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,結果溢出則拋出異常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) | 返回帶有第二個浮點參數(shù)符號的第一個浮點參數(shù) |
static float | copySign(float magnitude, float sign) | 返回帶有第二個浮點參數(shù)符號的第一個浮點參數(shù) |
static int | decrementExact(int a) | 返回a-1,如果結果溢出int則拋出異常 |
static long | decrementExact(long a) | 返回a-1,如果結果溢出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標準規(guī)定,計算兩個參數(shù)的余數(shù)運算 |
static int | incrementExact(int a) | 返回以1遞增的參數(shù),如果結果溢出 int則拋出異常 |
static long | incrementExact(long a) | 返回以1遞增的參數(shù),如果結果溢出 long則拋出異常 |
static double | log1p(double x) | 返回參數(shù)和的總和的自然對數(shù) |
static long | multiplyHigh(long x, long y) | 返回 long作為兩個64位因子的128位乘積的最高64位 |
static int | negateExact(int a) | 返回參數(shù)的否定,如果結果溢出 int則拋出異常 |
static long | negateExact(long a) | 返回參數(shù)的否定,如果結果溢出 long則拋出異常 |
static double | nextAfter(double start, double direction) | 返回第二個參數(shù)方向上第一個參數(shù)旁邊的浮點數(shù) |
static float | nextAfter(float start, double direction) | 返回第二個參數(shù)方向上第一個參數(shù)旁邊的浮點數(shù) |
static double | nextDown(double d) | 返回負無窮大方向上與 d相鄰的浮點值 |
static float | nextDown(float f) | 返回負無窮大方向上與 f相鄰的浮點值 |
static double | nextUp(double d) | 返回正無窮大方向上與 d相鄰的浮點值 |
static float | nextUp(float f) | 返回正無窮大方向上與 f相鄰的浮點值 |
static double | rint(double a) | 返回與 double值最接近的 double值,該值等于數(shù)學整數(shù) |
static double | scalb(double d, int scaleFactor) | 返回 d ×2 scaleFactor舍入,就像通過單個正確舍入的浮點乘以雙 scaleFactor值集的成員一樣 |
static float | scalb(float f, int scaleFactor) | 返回 f ×2 scaleFactor舍入,就像通過單個正確舍入的浮點乘以浮點值集的成員一樣 |
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ù)的差異,如果結果溢出 int則拋出異常 |
static long | subtractExact(long x, long y) | 返回參數(shù)的差異,如果結果溢出 long則拋出異常 |
static double | tanh(double x) | 返回 double值的雙曲正切值 |
static double | toDegrees(double angrad) | 將以弧度測量的角度轉換為以度為單位測量的近似等效角度 |
static int | toIntExact(long value) | 返回long參數(shù)的值; 如果值溢出int則拋出異常 |
static double | toRadians(double angdeg) | 將以度為單位測量的角度轉換為以弧度為單位測量的近似等效角度 |
static double | ulp(double d) | 返回參數(shù)的ulp大小 |
static float | ulp(float f) | 返回參數(shù)的ulp大小 |
總結
本篇文章就到這里了,希望能給你帶來幫助,也希望您能夠多多關注腳本之家的更多內(nèi)容!
相關文章
Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解
這篇文章主要介紹了Java中的ReentrantLock、ReentrantReadWriteLock、StampedLock詳解,讀寫鎖:一個資源能夠被多個讀線程訪問,或者被一個寫線程訪問但是不能同時存在讀寫線程,需要的朋友可以參考下2024-01-01Springboot 使用 maven-resources-plugin 打包變量替換ja
這篇文章主要介紹了Springboot 使用 maven-resources-plugin 打包變量替換jar沒有打包進去、Jar包沒有被使用的解決方法,本文給大家介紹的非常詳細,感興趣的朋友一起看看吧2024-08-08