strictfp

From Wikipedia, the free encyclopedia
Jump to: navigation, search

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[edit]

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 floats) or double (64-bit, used in Java doubles) 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.[1]

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[edit]

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 truncating all intermediate values to IEEE single precision and double precision, as occurred in earlier versions of the JVM.[1]

Usage[edit]

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 ...
}

Ideas for further improvement of Java's floating-point performance[edit]

The introduction of the strictfp keyword allowed Java to take advantage of the speed and precision of the extended precision floating-point operations supported by x86 CPUs (for all code not explicitly using the strictfp keyword), while those wanting total platform-independency could still have it by using the strictfp keyword.

There have been ideas that Java's floating-point behavior should be modified even more to allow faster or more precise calculations on platforms where this is possible.[4]

References[edit]

  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; Bill Joy; Guy L. Steele, Jr.; Gilad Bracha (2005). "15.4 FP-strict Expressions". The Java Language Specification, Third Edition. Addison-Wesley Professional. p. 411. ISBN 0-321-24678-0. Retrieved 2012-02-21. 
  4. ^ Kahan, W.; Joseph D. Darcy (1998-03-01). "How Java's Floating-Point Hurts Everyone Everywhere" (PDF). Retrieved 2006-12-09.