JAVA核心類庫筆記-java.lang.Math
阿新 • • 發佈:2021-02-09
技術標籤:java
Math
1.絕對值
變數和型別 | 方法 | 描述 |
---|---|---|
static double | abs(double a) | 返回 double值的絕對值。 |
static float | abs(float a) | 返回 float值的絕對值。 |
static int | abs(int a) | 返回 int值的絕對值。 |
static long | abs(long a) | 返回 long值的絕對值。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
System.out.println(Math.abs(-12));
System.out.println(Math.abs(-12.54));
System.out.println(Math.abs(-12.5));
System.out.println(Math.abs(-10000000000005L));
}
}
使用Math.abs()進行絕對值求解,先判斷數值是否小於零,小於則返回該值的負數,不小於則直接返回其本身。
2.計算型別
求和
變數和型別 | 方法 | 描述 |
---|---|---|
static int | addExact(int x, int y) | 返回其引數的總和,如果結果溢位即超出int值的範圍 ,int則丟擲異常。 |
static long | addExact(long x, long y) | 返回其引數的總和,如果結果溢位即超出long值的範圍 ,long則丟擲異常。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
System.out.println(Math.addExact(10,10);
System.out.println(Maths.addExact(10,10,10));
}
static class Maths{
public static int addExact(int x,int y,int z) {
return x + y + z;
}
}
}
Math.addExact()可以通過方法過載,實現多個數據計算求和,且遵循自動提升為更高階型別
求差
變數和型別 | 方法 | 描述 |
---|---|---|
static int | subtractExact(int x, int y) | 返回引數的差異,如果結果溢位 int則丟擲異常。 |
static long | subtractExact(long x, long y) | 返回引數的差異,如果結果溢位 long則丟擲異常。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
int a = -1 , b = 10;
System.out.println(Math.subtractExact(a, b));
}
}
其他與addExcat()一樣
乘積
變數和型別 | 方法 | 描述 |
---|---|---|
static int | subtractExact(int x, int y) | 返回引數的差異,如果結果溢位 int則丟擲異常。 |
static long | subtractExact(long x, long y) | 返回引數的差異,如果結果溢位 long則丟擲異常。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
int a = -1 , b = 10;
long c = 11L;
System.out.println(Math.multiplyExact(a, b));
System.out.println(Math.multiplyExact(a, c));
}
}
遞減遞增
變數和型別 | 方法 | 描述 |
---|---|---|
static int | decrementExact(int a) | 返回引數遞減1,如果結果溢位 int則丟擲異常。 |
static long | decrementExact(long a) | 返回引數遞減1,如果結果溢位 long則丟擲異常。 |
static int | incrementExact(int a) | 返回以1遞增的引數,如果結果溢位 int則丟擲異常。 |
static long | incrementExact(long a) | 返回以1遞增的引數,如果結果溢位 long則丟擲異常。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
int a = -1 , b = 10;
long c = 11L;
System.out.println(Math.decrementExact(a));
System.out.println(Math.decrementExact(c));
System.out.println(Math.incrementExact(a));
System.out.println(Math.incrementExact(c));
}
}
三角函式
變數和型別 | 方法 | 描述 |
---|---|---|
static double | asin(double a) | 返回值的反正弦值; 返回的角度在-pi / 2到pi / 2的範圍內。 |
static double | acos(double a) | 返回值的反餘弦值; 返回的角度在0.0到pi的範圍內。 |
static double | atan(double a) | 返回值的反正切值; 返回的角度在-pi / 2到pi / 2的範圍內。 |
*static double | atan2(double y, double x) | 返回從直角座標(轉換角度 theta x , y )為極座標 (R,θ-)。 |
static double | sin(double a) | 返回角度的三角正弦值。 |
*static double | sinh(double x) | 返回 double值的雙曲正弦值。 |
static double | tan(double a) | 返回角度的三角正切。 |
*static double | tanh(double x) | 返回 double值的雙曲正切值 |
static double | cos(double a) | 返回角度的三角餘弦值。 |
*static double | cosh(double a) | 返返回 double值的雙曲餘弦值。 |
" * ":表示暫時不理解使用原理
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
double a = 0;
double c = 3.14/6;
System.out.println(Math.acos(a));
System.out.println(Math.asin(a));
System.out.println(Math.atan(a));
System.out.println(Math.cos(a));
System.out.println(Math.sin(c));
System.out.println(Math.tan(a));
System.out.println(Math.tanh(a));
}
}
比大小
變數和型別 | 方法 | 描述 |
---|---|---|
static int | max(int a, int b) | 返回兩個 int值中較大的。 |
static long | max(long a, long b) | 返回兩個long值中較大的。 |
static double | max(double a, double b) | 返回兩個 double值中較大的。 |
static float | max(float a, float b) | 返回兩個 float值中較大的。 |
static int | max(int a, int b) | 返回兩個 int值中較小的。 |
static long | max(long a, long b) | 返回兩個 long值中較小的。 |
static double | max(double a, double b) | 返回兩個 double值中較小的 double 。 |
static float | max(float a, float b) | 返回兩個 float值中較小的。 |
/**
* @author 無音少年
*/
public class Test {
public static void main(String[] args) {
int a = 10, b = 11;
System.out.println(Math.max(a,b));
System.out.println(Math.min(a,b));
}
}