views:

107

answers:

3

I read the JVM specification for the fpstrict modifier but still don't fully understand what it means.

Can anyone enlighten me?

+7  A: 

Basically, it mandates that calculations involving the affected float and double variables have to follow the IEEE 754 spec to the letter, including for intermediate results.

This has the effect of:

  • Ensuring that the same input will always generate exactly the same result on all systems
  • The CPU may have to do some extra work, making it slightly slower
  • The results will be, in some cases, less accurate (much less, in extreme cases)

Edit: More specifically, many modern CPUs use 80 bit floating point arithmetic ("extended precision") internally. Thus, they can internally represent some numbers in denormalized form that would cause arithmetic overflow or underflow (yielding Infinity or zero, respectively) in 32 or 64bit floats; in borderline cases, 80 bit just allows to retain more precision. When such numbers occur as intermediate results in a calculation, but with an end result inside the range of numbers that can represented by 32/64bit floats, then you get a "more correct" result on machines that use 80bit float arithmetic than on machines that don't - or in code that uses strictfp.

Michael Borgwardt
Thanks Michael. In what cases might a floating point calculation differ from the IEEE754 spec?
Yuval A
+4  A: 

I like this answer, which hits the point

It ensures that your your calculations are equally wrong on all platforms.

Helper Method
A: 

strictfp is a keyword and can be used to modify a class or a method, but never a variable. Marking a class as strictfp means that any method code in the class will conform to the IEEE 754 standard rules for floating points. Without that modifier, floating points used in the methods might behave in a platform-dependent way. If you don't declare a class as strictfp, you can still get strictfp behavior on a method-by-method basis, by declaring a method as strictfp. If you don't know the IEEE 754 standard, now's not the time to learn it. You have, as we say, bigger fish to fry.

Sanjeev