February 01, 2012

strictfp is a Java keyword that restricts floating-point calculations in order to ensure portability.

strictfp is a Java keyword that restricts floating-point calculations in
order to ensure portability. The modifier was added to the Java programming
language with the Java virtual machine version 1.2.

Older versions of JVMs were always use strict floating-point, means all
values used during floating-point calculations are made in the
IEEE-standard float or double sizes. This sometimes result in a numeric
overflow or underflow during calculation, even if the end result would be a
valid number. However JVM 1.2, does not restrict to all the numbers used in
floating-point calculations limited to the standard floator double
precision.

While some applications, might require having precisely the same
floating-point behavior for every platform, even if some platforms could
handle more precision. In such situations, a programmer is free to use the
modifier strictfp to make sure that calculations are performed as in the
earlier versions?only with floats and doubles.

The modifier can be combined with classes, interfaces and methods.

Strict floating point behavior are typically used by compile-time constant
expressions. While other expressions use strict floating-point math
whenever they are contained in a method, interface, or class which uses
the strictfp modifier in its declaration.
The strictfp keyword may be applied to both classes and methods. For
classes (and interfaces), it specifies that all of the expressions in all
of the methods of the class are FP-strict. For methods, it specifies that
all of the expressions in that method are FP-strict.

So, what the heck does FP-strict really mean? Well, that actually gets
pretty hairy. For the full, gory details check out section 2.18 of Lindholm
and Yellin's The Java Virtual Machine Specification Second Edition. If you
do not specifystrictfp then the compiler & runtime system may be more
aggressive in how they deal with floating-point expressions. Whereas
with strictfp specified, the compiler and runtime system must toe the line
very precisely according to the IEEE-754 floating-point specification.

Basically, what it all boils down to is whether or not you care that the
results of floating-point expressions in your code are fast or predictable.
For example, if you need the answers that your code comes up with which
uses floating-point values to be consistent across multiple platforms then
use strictfp.
For the exam, you need to know only that
strictfp is a keyword and can be used to modify a class or a method, but
never a variable. Marking a class as strictfp means that any method code in
the class will conform to the IEEE 754 standard rules for floating points.
Without that modifier, floating points used in the methods might behave in
a platform dependent way. If you don't declare a class as strictfp, you can
still get strictfp behavior on a method-by-method basis, by declaring a
method as strictfp. If you don't know the IEEE 754 standard, now's not the
time to learn it. You have, as we say, bigger fish to fry.

1 comment:

  1. Warning: concerning performance, you should not mix code with / without the strictfp keyword because it can lead to a 20% - 30% performance penalty (tested on JDK 1.7 on exact fourier transforms).

    ReplyDelete

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts