Skip to content
Advertisement

When should I use the “strictfp” keyword in java?

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?

Advertisement

Answer

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.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement