views:

3528

answers:

4

Ok, I've looked up what this does, but does anyone actually have an example of when you would use the "strictfp" keyword in java? Has anyone actually found a use for this?

Would there be any side-effects of just putting it on all my floating point operations?

+5  A: 

Wikipedia actually has a good article about this topic here, with a link to the Java specification.

Reading between the lines, the implication is that if you don't specify strictfp, then the JVM and JIT compiler have license to compute your floating-point calculations however they want. In the interest of speed, they will most likely delegate the computation to your processor. With strictfp on, the computations have to conform to IEEE 754 arithmetic standards, which, in practice, probably means that the JVM will do the computation.

So why would you want to use strictfp? One scenario I can see is in a distributed application (or multiplayer game) where all floating-point calculations need to be deterministic no matter what the underlying hardware or CPU is. What's the trade-off? Most likely execution time.

MattK
+11  A: 

Strictfp ensures that you get exactly the same results from your floating point calculations on every platform. If you don't use strictfp, the JVM implementation is free to use extra precision where available.

From the JLS:

Within an FP-strict expression, all intermediate values must be elements of the float value set or the double value set, implying that the results of all FP-strict expressions must be those predicted by IEEE 754 arithmetic on operands represented using single and double formats. Within an expression that is not FP-strict, some leeway is granted for an implementation to use an extended exponent range to represent intermediate results; the net effect, roughly speaking, is that a calculation might produce "the correct answer" in situations where exclusive use of the float value set or double value set might result in overflow or underflow.

In other words, it's about making sure that Write-Once-Run-Anywhere actually means Write-Once-Get-Equally-Wrong-Results-Everywhere.

With strictfp your results are portable, without it they are more likely to be accurate.

Dan Dyer
+3  A: 
Michael Myers
+2  A: 

As the other answers mentioned it cause the intermediate floating point results to conform to the IEEE specification. In particular x86 processors can store intermediate results with different precision from the IEEE spec. The situation gets more complicated when the JIT optimizes a particular computation; the order the instructions could be different each time resulting in slightly different rounding.

The overhead incurred by strictfp likely to be very processor and JIT dependent. This wikipedia article on SSE2 seems to have some insight into the problem. So if the JIT can generate SSE instructions to perform a calculation it seems that strictfp will not have any overhead.

In my current project there are a few places where I use strictfp. There is a point where potential cosmic rays need to be removed from pixel values. If some outside researcher has the the same pixel value and cosmic ray in front them they should get the same resulting value as our software.

Sean McCauliff