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?
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. Withstrictfp
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.Here are several references:
jGuru: What is the strictfp modifier for? When would I consider using it?
strictfp - Java Glossary
And finally the actual Java Language Specification, §15.4 FP-strict Expressions:
I've never personally had a use for it, though.
May below example help in understanding this more clear : In java whenever we are using looking for precise information for any operation e.g. if we do double num1 = 10e+102; double num2 = 8e+10 ; result = num1+ num2;
It all began with a story,
When java was being developed by James Gosling, Herbert and rest of his team. They had this crazy thing in mind called platform independency. They wanted to make oak(Java) so much better that it would run exactly same on any machine having different instruction set, even running different operating systems. But, there was a problem with decimal point numbers also known as floating point and double in programming languages. Some machines were built targeting efficiency while rest were targeting accuracy. So, the later(more accurate) machines had size of floating point as 80 bits while the former(more efficient/faster) machines had 64 bit doubles. But, this was against there core idea of building a platform independent language. Also, this might lead to loss of precision/data when a code is built on some machine(having double of 64 bit size) and run on another kind of machine(having double of 80 bit size).
Up-Sizing can be tolerated but Down-Sizing can't be. So, they came across a concept of strictfp i.e. strict floating point. If you use this keyword with a class/function then its floating point and doubles have a consistent size over any machine. i.e. 32/64 -bit respectively.
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:
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.
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.