Jump to content

Strictfp: Difference between revisions

From Wikipedia, the free encyclopedia
Content deleted Content added
Iamnitin (talk | contribs)
No edit summary
m Added reflist in.
(One intermediate revision by the same user not shown)
Line 1: Line 1:
{{lowercase|title=strictfp}}
{{lowercase|title=strictfp}}
<code>'''strictfp'''</code> is a [[Java keyword]] used to restrict floating-point calculations to ensure portability. The modifier was introduced into the [[Java (programming language)|Java programming language]] with the [[Java virtual machine]] version 1.2.
<code>'''strictfp'''</code> is a [[Java keyword|keyword]] in the [[Java (programming language)|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.


==Basis==
In older JVMs, [[floating-point]] calculations were always strict floating-point, meaning all values used during floating-point calculations are made in the IEEE-standard ''float'' or ''double'' sizes. This could sometimes result in a numeric [[Arithmetic_overflow|overflow]] or [[Arithmetic_underflow|underflow]] in the middle of a calculation, even if the end result would be a valid number. Since version 1.2 of the JVM, floating-point calculations do not require that all numbers used in computations are themselves limited to the standard ''float'' or ''double'' precision.


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 <code>float</code>s) or double (64-bit, used in Java <code>double</code>s) precision. 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. As a consequence, errors of calculation, [[Arithmetic_overflow|overflows]] and [[Arithmetic_underflow|underflows]], could occur. Whether or not an error had occurred, the calculation would always return a valid number; if an overflow or underflow had occurred, that number would be incorrect. Hence, whether an error had occurred was typically not obvious.
However, for some applications, a programmer might require every platform to have precisely the same floating-point behavior, even if some platforms could handle more precision. In that case, the programmer can use the modifier <code>strictfp</code> to ensure that calculations are performed as in the earlier versions—only with ''float''s and ''double''s.
Since JVM 1.2, intermediate computations are not limited to the standard 32- and 64- bit precisions. On platforms that can handle other representations, those representations can be used, preventing overflows and underflows, 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 <code>strictfp</code> modifier accomplishes this by truncating all intermediate values to IEEE single- and double- precision, as occurred in earlier versions of the JVM<ref>{{cite book |last=Flanagan |first=David |authorlink=David Flanagan |title=Java in a Nutshell |edition=Fifth |publisher=[[O'Reilly Media]] |date=March 2005 |isbn=978-0-596-00773-7 |url=http://oreilly.com/catalog/9780596007737 |accessdate=2010-03-03}}</ref>.


==Usage==
The modifier can be used in combination with [[Class (computer science)|classes]], [[Interface (computer science)|interfaces]] and non-abstract [[Method (computer science)|methods]].

Programmers can use the modifier <code>strictfp</code> 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. This can be extremely useful when comparing floating-point numbers{{citation needed}}.

It can be used on [[Class (computer science)|classes]], [[Interface (computer science)|interfaces]] and non-abstract [[Method (computer science)|methods]].<ref>{{cite book|first=Herbert|last=Schildt|title=Java: A Beginner's Guide|edition=4|publisher=McGraw-Hill Companies|date=2007|accessdate=2010-04-22|isbn=978-0-07-226384-8}}</ref> 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{{citation needed}}

'''Examples'''


Compile-time constant expressions must always use strict floating-point behavior. Other expressions use strict floating-point math if they are contained in a method, interface, or class which uses the <code>strictfp</code> modifier in its declaration. In a class declaration, the modifier can be placed like this:
<source lang="java">
<source lang="java">
public strictfp class MyFPclass {
public strictfp class MyFPclass {
Line 14: Line 21:
}
}
</source>
</source>

Examples:
The [[Java package]] java.lang.Math class contains these strictfp methods:
<source lang="java">
<source lang="java">
// From java.lang.Math class............
public static strictfp double abs(double);
public static strictfp double abs(double);
public static strictfp int max(int, int);
public static strictfp int max(int, int);
Line 24: Line 31:
public static strictfp int min(int, int);
public static strictfp int min(int, int);
</source>
</source>

==Behaviors and restrictions==
Strictfp is sometimes viewed as a problem of Java<ref>{{cite web | url=http://www.cs.berkeley.edu/~wkahan/JAVAhurt.pdf | title=How Java's Floating-Point Hurts Everyone Everywhere | first=W. | last=Kahan | coauthors=Joseph D. Darcy | format=PDF | date=1998-03-01 | accessdate=2006-12-09}}</ref><ref>{{cite web | url=http://java.sun.com/docs/books/jls/third_edition/html/typesValues.html#4.2.3 | title=Types, Values, and Variables | publisher=Sun Microsystems | accessdate=2006-12-09}}</ref>.

== References ==
== References ==


* http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#24465
* http://java.sun.com/docs/books/vmspec/2nd-edition/html/Concepts.doc.html#24465
{{reflist}}


[[Category:Java programming language]]
[[Category:Java programming language]]

Revision as of 21:33, 22 April 2010

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.

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. 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. As a consequence, errors of calculation, overflows and underflows, could occur. Whether or not an error had occurred, the calculation would always return a valid number; if an overflow or underflow had occurred, that number would be incorrect. Hence, whether an error had occurred was typically not obvious. Since JVM 1.2, intermediate computations are not limited to the standard 32- and 64- bit precisions. On platforms that can handle other representations, those representations can be used, preventing overflows and underflows, 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].

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. This can be extremely useful when comparing floating-point numbers[citation needed].

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[citation needed]

Examples

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

The Java package java.lang.Math class contains these strictfp methods:

 public static strictfp double abs(double);
 public static strictfp int max(int, int);
 public static strictfp long max(long, long);
 public static strictfp float max(float, float);
 public static strictfp double max(double, double);
 public static strictfp int min(int, int);

Behaviors and restrictions

Strictfp is sometimes viewed as a problem of Java[3][4].

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. {{cite book}}: |access-date= requires |url= (help)
  3. ^ Kahan, W. (1998-03-01). "How Java's Floating-Point Hurts Everyone Everywhere" (PDF). Retrieved 2006-12-09. {{cite web}}: Unknown parameter |coauthors= ignored (|author= suggested) (help)
  4. ^ "Types, Values, and Variables". Sun Microsystems. Retrieved 2006-12-09.