Question

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: When should I use the "strictfp" keyword in java?

Was it helpful?

Solution

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.

OTHER TIPS

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.

strictfp its very special.

Let see with a A space Agency example.

The code written uses very specific scientific Numbers, Now this Numbers are in multi-placed decimals.

These decimals value effects the trajectory of the space-ship path , which is effected by my constants such as friction, gravity, heat, moisture, sun-ray received , electricity generated , multiple gravitational body, velocity, flue efficient.

The main point is that when the data is send for evaluation to keep track of the ship, it should return the value which is same for different platform else we will receive data which is slightly different in decimal value.

But this is not consider as small difference, as it will result in the ship to divert from its trajectory, Remember deferential calculus, each small dot make trajectory, so our calculation must be very specific and consistent for even different platform, it must have standers and precis accuracy.

For this requirement we have strictfp in java.

You want a program to demonstrate it, its difficult to show this accurately with a small program.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top