下面的示例演示了一個使用 strictfp 修飾符聲明的類。
- // Example of precision control with strictfp
- public strictfp class MyClass
- {
- public MyClass(){}
- public static void main(String[] args)
- {
- float aFloat = 0.6710339f;
- double aDouble = 0.04150553411984792d;
- double sum = aFloat + aDouble;
- float quotient = (float)(aFloat / aDouble);
- System.out.println("float: " + aFloat);
- System.out.println("double: " + aDouble);
- System.out.println("sum: " + sum);
- System.out.println("quotient: " + quotient);
- }
- }
// Example of precision control with strictfp
public strictfp class MyClass
{
public MyClass(){}
public static void main(String[] args)
{
float aFloat = 0.6710339f;
double aDouble = 0.04150553411984792d;
double sum = aFloat + aDouble;
float quotient = (float)(aFloat / aDouble);
System.out.println("float: " + aFloat);
System.out.println("double: " + aDouble);
System.out.println("sum: " + sum);
System.out.println("quotient: " + quotient);
}
}
- float: 0.6710339
- double: 0.04150553411984792
- sum: 0.71253945297742238
- quotient: 16.1673355
float: 0.6710339
double: 0.04150553411984792
sum: 0.71253945297742238
quotient: 16.1673355
- // Example of precision control with strictfp:
- public class MyClass2
- {
- public float aFloat;
- public double aDouble;
- public MyClass2(){}
- public strictfp double add(float a, double b)
- {
- return (a + b);
- }
- public static void main(String[] args)
- {
- MyClass2 myClass2 = new MyClass2();
- myClass2.aFloat = 0.6710339f;
- myClass2.aDouble = 0.04150553411984792d;
- double sum = myClass2.add(myClass2.aFloat, myClass2.aDouble);
- System.out.println("float: " + myClass2.aFloat);
- System.out.println("double: " + myClass2.aDouble);
- System.out.println("sum: " + sum);
- }
- }
// Example of precision control with strictfp:
public class MyClass2
{
public float aFloat;
public double aDouble;
public MyClass2(){}
public strictfp double add(float a, double b)
{
return (a + b);
}
public static void main(String[] args)
{
MyClass2 myClass2 = new MyClass2();
myClass2.aFloat = 0.6710339f;
myClass2.aDouble = 0.04150553411984792d;
double sum = myClass2.add(myClass2.aFloat, myClass2.aDouble);
System.out.println("float: " + myClass2.aFloat);
System.out.println("double: " + myClass2.aDouble);
System.out.println("sum: " + sum);
}
}
- float: 0.6710339
- double: 0.04150553411984792
- sum: 0.71253945297742238
float: 0.6710339
double: 0.04150553411984792
sum: 0.71253945297742238
自Java2以來,Java語言增加了一個關鍵字strictfp。strictfp的意思是FP-strict,也就是說精確浮點的意思。在Java虛擬機進行浮點運算時,如果沒有指定strictfp關鍵字時,Java的編譯器以及運行環境在對浮點運算的表達式是採取一種近似於我行我素的行爲來完成這些操作,以致於得到的結果往往無法令你滿意。而一旦使用了strictfp來聲明一個類、接口或者方法時,那麼所聲明的範圍內Java的編譯器以及運行環境會完全依照浮點規範IEEE-754來執行。因此如果你想讓你的浮點運算更加精確,而且不會因爲不同的硬件平臺所執行的結果不一致的話,那就請用關鍵字strictfp。
你可以將一個類、接口以及方法聲明爲strictfp,但是不允許對接口中的方法以及構造函數聲明strictfp關鍵字,例如下面的代碼:
1. 合法的使用關鍵字strictfp
public strictfp class FpDemo1 ...{
strictfp void f() ...{}
}
2. 錯誤的使用方法
strictfp void f();
}
public class FpDemo2 ...{
strictfp FpDemo2() ...{}
}
一旦使用了關鍵字strictfp來聲明某個類、接口或者方法時,那麼在這個關鍵字所聲明的範圍內所有浮點運算都是精確的,符合IEEE-754規範的。例如一個類被聲明爲strictfp,那麼該類中所有的方法都是strictfp的。