您的位置:首页 > 编程语言 > Java开发

Strictfp —— Java 关键字

2010-08-22 21:29 211 查看
  Strictfp —— Java 关键字。

  strictfp, 即 strict float point (精确浮点)。

  strictfp 关键字可应用于类、接口或方法。使用 strictfp 关键字声明一个方法时,该方法中所有的float和double表达式都严格遵守FP-strict的限制,符合IEEE-754规范。当对一个类或接口使用 strictfp 关键字时,该类中的所有代码,包括嵌套类型中的初始设定值和代码,都将严格地进行计算。严格约束意味着所有表达式的结果都必须是 IEEE 754 算法对操作数预期的结果,以单精度和双精度格式表示。

  如果你想让你的浮点运算更加精确,而且不会因为不同的硬件平台所执行的结果不一致的话,可以用关键字strictfp.

  示例 1

下面的示例演示了一个使用 strictfp 修饰符声明的类。

  

Java代码

// 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);

  }

  }


  示例输出

Java代码

float: 0.6710339

double: 0.04150553411984792

sum: 0.71253945297742238

quotient: 16.1673355

float: 0.6710339

  double: 0.04150553411984792

  sum: 0.71253945297742238

  quotient: 16.1673355


  示例 2

  下面的示例演示了一个使用 strictfp 修饰符声明的方法。

Java代码

// 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);

  }

  }


  示例输出

Java代码

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




strictfp
interface A
...{}




public
strictfp
class FpDemo1
...{




strictfp
void f()
...{}


}

2. 错误的使用方法




interface
A ...{


strictfp
void f();


}




public
class FpDemo2
...{




strictfp
FpDemo2() ...{}


}

一旦使用了关键字strictfp来声明某个类、接口或者方法时,那么在这个关键字所声明的范围内所有浮点运算都是精确的,符合IEEE-754规范的。例如一个类被声明为strictfp,那么该类中所有的方法都是strictfp的。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: