I know this is a duplicate question. but i want know it with an example. so can anyone explain it with an example?
Link to duplicate post: http://stackoverflow.com/questions/517915/when-to-use-strictfp-keyword-in-java
I know this is a duplicate question. but i want know it with an example. so can anyone explain it with an example?
Link to duplicate post: http://stackoverflow.com/questions/517915/when-to-use-strictfp-keyword-in-java
When you use the strictfp modifier on a method or class, the compiler generates code that adheres strictly to the Java spec for identical results on all platforms. Without strictfp, is it is slightly laxer, but not so lax as to use the guard bits in the Pentium to give 80 bits of precision. With JET, there is an option to use the full 80 bits in calculation.
Well, girinie's answer sums it up nicely, but if you're looking for an example ..
Let's say you made a game which allows for highscore submissions, but only after the server validates the score. One method to validate the score is to have the client send the keypresses (and their timestamps) to the server. Ideally the server would then play through the exact same game and obtain the same score.
Now, let's say your game has some physics that can alter the outcome of the game (e.g. an explosion throwing random debris which COULD hurt you, if it hits).
The physics of that game potentially could be different on the server (even if it's only slightly) than the client (e.g., if converting to integers for collision detection, it was rounded down instead of up). On an edge case like that, you could have a situation where the client game was not hit by debris from the explosion, but the server game was - and now you have a difference in score, which could incorrectly invalidate a highscore submission
While strictfp
is certainly not a silver bullet, it goes a long way in imposing some consistency of 'replaying' instructions across different platforms.
This is what the java language spec says:
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.
What that means is this: Floating point arithmetic (i.e. calculations involving the float
and double
types) in Java is specified to adher to the IEEE 754 standard, which says exactly how to represent FP numbers. The problem is that modern CPUs internally use FP arithmetic that does not adher to that standard for intermediate results - this is not generally a problem because it's actually both faster and yields more exact results. But it could mean that a program produces slightly different results depending on which hardware it runs on - which is against the the basic promise of Java's platform-independance.
The strictfp
keyword allows you to ensure that this promise is kept and the program will have exactly the same result, no matter where it runs - but this comes at the cost of lower performance on hardware where extra effort must be expended to make the FP calculations adher to IEEE 754 on all intermediate results.
Most of the time, you'd rahter have better performance rather than guaranteed identical results across platforms, and that's why strictfp
behaviour is optional. Actually it was made optional in Java 1.4 after JVM implementors realized that they had to make the CPU do extra work to adher to the spec when most of the time such strict adherence had no advantages.