3.8 Strict or Nonstrict Floating-Point Arithmetic

   

 
Java Number Cruncher: The Java Programmer's Guide to Numerical Computing
By Ronald  Mak

Table of Contents
Chapter  3.   The Floating-Point Standard

3.8 Strict or Nonstrict Floating-Point Arithmetic

Whenever Java performs a floating-point operation, we know that the final result will be in the IEEE 754 single-precision or double-precision format, after being properly rounded to the nearest representable value. But if an expression contains multiple operations, what happens to the intermediate values?

Java provides two modes for evaluating floating-point expressions, FP-strict and non-FP-strict. In FP-strict mode, all the intermediate values of a computation must also be properly rounded and be in the appropriate IEEE 754 format. One important consequence of FP-strict is that your program is guaranteed to produce exactly the same results across all Java virtual machine implementations .

On the other hand, non-FP-strict mode allows the Java virtual machine, during the calculation of the intermediate values, to take better advantage of the underlying floating-point hardware, such as any "extended formats" that differ from the IEEE 754 standard. This may result in fewer overflows, perhaps less precision, or faster floating-point arithmetic. The final result value must still be properly rounded and be in the appropriate IEEE 754 format. However, there is no longer a guarantee that the final values will be exactly the same across all virtual machine implementations.

Compile-time constant floating-point expressions are always evaluated in FP-strict mode. Otherwise, by using the strictfp keyword, you can indicate which mode you want to be in effect when an expression is evaluated at run time. You can apply the keyword as a modifier of a class, an interface, or a method. Some examples are

 public strictfp class MatrixMath { ... } strictfp interface Integrand { ... } public class ComplexMath {     public strictfp float norm() { ... }     ... } 

If a method is modified by strictfp, then all the floating-point expressions in the method will be evaluated in FP-strict mode. If a class or an interface is modified by strictfp, then all the expressions in the class will be evaluated in FP-strict mode. A class that is FP-strict does not necessarily mean that its subclasses are each subclass needs to be modified (or not) individually.

A particular Java virtual machine implementation may choose to always use the FP-strict mode, even in the absence of the strictfp modifier. Non-FP-strict mode is an option, not a requirement, for a virtual machine.


   
Top


Java Number Cruncher. The Java Programmer's Guide to Numerical Computing
Java Number Cruncher: The Java Programmers Guide to Numerical Computing
ISBN: 0130460419
EAN: 2147483647
Year: 2001
Pages: 141
Authors: Ronald Mak

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net