1. 程式人生 > >深入學習java原始碼之Math.toRadians()與 Math.toDegrees()

深入學習java原始碼之Math.toRadians()與 Math.toDegrees()

深入學習java原始碼之Math.toRadians()與 Math.toDegrees()

strictfp關鍵字, 即 strict float point (精確浮點)。
strictfp 的意思是FP-strict,也就是說精確浮點的意思。在Java虛擬機器進行浮點運算時,如果沒有指定strictfp關鍵字時,Java的編譯器以及運 行環境在對浮點運算的表示式是採取一種近似於我行我素的行為來完成這些操作,以致於得到的結果往往無法令你滿意。而一旦使用了strictfp來宣告一個 類、介面或者方法時,那麼所宣告的範圍內Java的編譯器以及執行環境會完全依照浮點規範IEEE-754來執行。因此如果你想讓你的浮點運算更加精確, 而且不會因為不同的硬體平臺所執行的結果不一致的話,那就請用關鍵字strictfp。
如果你想讓你的浮點運算更加精確,而且不會因為不同的硬體平臺所執行的結果不一致的話,可以用關鍵字strictfp. 

Modifier and Type Method and Description
static double toDegrees(double angrad)

將以弧度測量的角度轉換為以度為單位的近似等效角度。

static double toRadians(double angdeg)

將以度為單位的角度轉換為以弧度測量的大致相等的角度。

java原始碼

public final class Math {

    private Math() {}

    public static final double E = 2.7182818284590452354;

    public static final double PI = 3.14159265358979323846;
	
    public static double toRadians(double angdeg) {
        return angdeg / 180.0 * PI;
    }	
	
    public static double toDegrees(double angrad) {
        return angrad * 180.0 / PI;
    }
}	
public final class StrictMath {

    private StrictMath() {}

    public static final double E = 2.7182818284590452354;

    public static final double PI = 3.14159265358979323846;

    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;
    }	
}