深入學習java原始碼之Math.sin()與 Math.sqrt()
深入學習java原始碼之Math.sin()與 Math.sqrt()
native關鍵字
凡是一種語言,都希望是純。比如解決某一個方案都喜歡就單單這個語言來寫即可。Java平臺有個使用者和本地C程式碼進行互操作的API,稱為JNI
native關鍵字告訴編譯器(其實是JVM)呼叫的是該方法在外部定義,這裡指的是C。
Modifier and Type | Method and Description |
---|---|
static double |
acos(double a) 返回值的反餘弦值; 返回的角度在0.0到pi的範圍內。 |
static int |
addExact(int x, int y) 返回其引數的總和,如果結果溢位int,則丟擲 |
static long |
addExact(long x, long y) 返回其引數的總和,如果結果溢位long,則丟擲 |
static double |
asin(double a) 返回值的正弦值; 返回角度在pi / 2到pi / 2的範圍內。 |
static double |
atan(double a) 返回值的反正切值; 返回角度在pi |
static double |
atan2(double y, double x) 返回從直角座標(轉換角度 theta |
static double |
cbrt(double a) 返回 |
static double |
ceil(double a) 返回大於或等於引數的最小(最接近負無窮大) |
static double |
使用第二個浮點引數的符號返回第一個浮點引數。 |
static float |
copySign(float magnitude, float sign) 使用第二個浮點引數的符號返回第一個浮點引數。 |
static double |
cos(double a) 返回角度的三角餘弦。 |
static double |
cosh(double x) 返回的雙曲餘弦 |
static double |
exp(double a) 返回尤拉的數字 e提高到一個 |
static double |
expm1(double x) 返回 e x -1。 |
static double |
hypot(double x, double y) 返回sqrt( x 2 + y 2 ),沒有中間溢位或下溢。 |
static double |
log(double a) 返回的自然對數(以 e為底) |
static double |
log10(double a) 返回一個 |
static double |
log1p(double x) 返回引數和1的和的自然對數。 |
static double |
pow(double a, double b) 將第一個引數的值返回到第二個引數的冪。 |
static double |
random() 返回值為 |
static double |
rint(double a) 返回與引數最接近值的 |
static long |
round(double a) 返回引數中最接近的 |
static int |
round(float a) 返回引數中最接近的 |
static double |
sin(double a) 返回角度的三角正弦。 |
static double |
sinh(double x) 返回的雙曲正弦 |
static double |
sqrt(double a) 返回的正確舍入正平方根 |
static double |
tan(double a) 返回角度的三角正切。 |
static double |
tanh(double x) 返回的雙曲正切 |
static double |
ulp(double d) 返回引數的ulp的大小。 |
static float |
ulp(float f) 返回引數的ulp的大小。 |
java原始碼
public final class Math {
private Math() {}
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
public static double sin(double a) {
return StrictMath.sin(a); // default impl. delegates to StrictMath
}
public static double cos(double a) {
return StrictMath.cos(a); // default impl. delegates to StrictMath
}
public static double tan(double a) {
return StrictMath.tan(a); // default impl. delegates to StrictMath
}
public static double asin(double a) {
return StrictMath.asin(a); // default impl. delegates to StrictMath
}
public static double acos(double a) {
return StrictMath.acos(a); // default impl. delegates to StrictMath
}
public static double atan(double a) {
return StrictMath.atan(a); // default impl. delegates to StrictMath
}
public static double exp(double a) {
return StrictMath.exp(a); // default impl. delegates to StrictMath
}
public static double log(double a) {
return StrictMath.log(a); // default impl. delegates to StrictMath
}
public static double log10(double a) {
return StrictMath.log10(a); // default impl. delegates to StrictMath
}
public static double sqrt(double a) {
return StrictMath.sqrt(a); // default impl. delegates to StrictMath
// Note that hardware sqrt instructions
// frequently can be directly used by JITs
// and should be much faster than doing
// Math.sqrt in software.
}
public static double cbrt(double a) {
return StrictMath.cbrt(a);
}
public static double IEEEremainder(double f1, double f2) {
return StrictMath.IEEEremainder(f1, f2); // delegate to StrictMath
}
public static double atan2(double y, double x) {
return StrictMath.atan2(y, x); // default impl. delegates to StrictMath
}
public static double pow(double a, double b) {
return StrictMath.pow(a, b); // default impl. delegates to StrictMath
}
public static double sinh(double x) {
return StrictMath.sinh(x);
}
public static double cosh(double x) {
return StrictMath.cosh(x);
}
public static double tanh(double x) {
return StrictMath.tanh(x);
}
public static double hypot(double x, double y) {
return StrictMath.hypot(x, y);
}
public static double expm1(double x) {
return StrictMath.expm1(x);
}
public static double log1p(double x) {
return StrictMath.log1p(x);
}
}
public final class StrictMath {
private StrictMath() {}
public static final double E = 2.7182818284590452354;
public static final double PI = 3.14159265358979323846;
public static native double sin(double a);
public static native double cos(double a);
public static native double tan(double a);
public static native double asin(double a);
public static native double acos(double a);
public static native double atan(double a);
public static strictfp double toRadians(double angdeg) {
// Do not delegate to Math.toRadians(angdeg) because
// this method has the strictfp modifier.
return angdeg / 180.0 * PI;
}
public static strictfp double toDegrees(double angrad) {
// Do not delegate to Math.toDegrees(angrad) because
// this method has the strictfp modifier.
return angrad * 180.0 / PI;
}
public static native double exp(double a);
public static native double log(double a);
public static native double log10(double a);
public static native double sqrt(double a);
public static native double cbrt(double a);
public static native double IEEEremainder(double f1, double f2);
public static native double atan2(double y, double x);
public static native double pow(double a, double b);
public static native double sinh(double x);
public static native double cosh(double x);
public static native double tanh(double x);
public static native double hypot(double x, double y);
public static native double expm1(double x);
public static native double log1p(double x);
}