strictfp

strictfp is a keyword in the Java programming language that restricts floating-point calculations to ensure portability. The strictfp command was introduced into Java with the Java virtual machine (JVM) version 1.2 and is available for use on all currently updated Java VMs.

Basis

The IEEE standard IEEE 754 specifies a standard method for both floating-point calculations and storage of floating-point values in either single (32-bit, used in Java's float) or double (64-bit, used in Java's double) precision, and, for intermediate calculations, also extended precision formats.

Prior to JVM 1.2, floating-point calculations were strict; that is, all intermediate floating-point results were represented as IEEE single or double precisions only. As a consequence, errors of calculation (round-off errors), overflows and underflows, would occur with greater frequency than in architectures which did intermediate calculations in greater precision. The arithmetic issues were a real problem on early Java VMs, and many other solutions besides the use of this instruction were proposed.

Since JVM 1.2, intermediate computations are not limited to the standard 32 bit and 64 bit precisions. On platforms that can handle other representations e.g. 80-bit double extended on x86 or x86-64 platforms, those representations can be used, helping to prevent round-off errors and overflows, thereby increasing precision.

How it works

For some applications, a programmer might need every platform to have precisely the same floating-point behaviour, even on platforms that could handle greater precision. However, if this level of precision is not necessary the VM does not use intermediates by default.

From the VM perspective, turning on this higher precision means the following:

Precision Intermediate
32 bits 64 bits
64 bits 80 bits (if available)

The strictfp modifier accomplishes this by rounding all intermediate values to IEEE single precision and double precision, as occurred in earlier versions of the JVM.[1]

Usage

Programmers can use the modifier strictfp to ensure that calculations are performed as in the earlier versions; that is, only with IEEE single and double precision types used. Using strictfp guarantees that results of floating-point calculations are identical on all platforms.

It can be used on classes, interfaces and non-abstract methods.[2] When applied to a method, it causes all calculations inside the method to use strict floating-point math. When applied to a class, all calculations inside the class use strict floating-point math. Compile-time constant expressions must always use strict floating-point behavior.[3]

Examples

public strictfp class MyFPclass { 
    // ... contents of class here ...
}

References

  1. Flanagan, David (March 2005). Java in a Nutshell (Fifth ed.). O'Reilly Media. ISBN 978-0-596-00773-7. Retrieved 2010-03-03.
  2. Schildt, Herbert (2007). Java: A Beginner's Guide (4 ed.). McGraw-Hill Companies. ISBN 978-0-07-226384-8.
  3. Gosling, James; Joy, Bill; Steele, Guy L. Jr.; Bracha, Gilad (2005). "15.4 FP-strict Expressions". The Java Language Specification, Third Edition. Addison-Wesley Professional. p. 411. ISBN 0-321-24678-0. Retrieved 2016-03-22.
This article is issued from Wikipedia - version of the 7/8/2016. The text is available under the Creative Commons Attribution/Share Alike but additional terms may apply for the media files.