Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does fpstrict do in Java?

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

Can anyone enlighten me?

like image 225
Yuval Adam Avatar asked Mar 24 '10 09:03

Yuval Adam


3 Answers

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.

like image 57
Michael Borgwardt Avatar answered Sep 20 '22 04:09

Michael Borgwardt


I like this answer, which hits the point:

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

like image 41
helpermethod Avatar answered Sep 21 '22 04:09

helpermethod


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.

like image 45
Sanjeev Avatar answered Sep 20 '22 04:09

Sanjeev