现在的位置: 首页 > 综合 > 正文

java关键字——strictfp

2014年09月05日 ⁄ 综合 ⁄ 共 1450字 ⁄ 字号 评论关闭

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

strictfp关键字可应用于类、接口、方法。

 

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

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

 

 

示例 1

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

  public strictfp class MyClass

  {

  public MyClass(){}

  public static voidmain(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

 

示例 2

下面的示例演示了一个使用 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 voidmain(String[] args)

  {

  MyClass2 myClass2 = newMyClass2();

  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

 

 

原帖地址:http://baike.baidu.com/view/1866622.htm

 

抱歉!评论已关闭.