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. It was introduced into Java with the Java virtual machine (JVM) version 1.2.

Contents

[edit] 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 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, could occur. Since JVM 1.2, intermediate computations are not limited to the standard 32- 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. For some applications, a programmer might need every platform to have precisely the same floating-point behavior, even on platforms that could handle greater precision. The strictfp modifier accomplishes this by truncating all intermediate values to IEEE single- and double- precision, as occurred in earlier versions of the JVM[1].

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

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

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

[edit] References

  1. ^ Flanagan, David (March 2005). Java in a Nutshell (Fifth ed.). O'Reilly Media. ISBN 978-0-596-00773-7. http://oreilly.com/catalog/9780596007737. 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. http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.4. Retrieved 2012-02-21. 
  4. ^ Kahan, W.; Joseph D. Darcy (1998-03-01). "How Java's Floating-Point Hurts Everyone Everywhere" (PDF). http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf. Retrieved 2006-12-09. 
Personal tools
Namespaces
Variants
Actions
Navigation
Interaction
Toolbox
Print/export
Languages