开发者

What does fpstrict do in Java?

开发者 https://www.devze.com 2022-12-23 19:09 出处:网络
I read the JVM specification for the fps开发者_开发知识库trict modifier but still don\'t fully understand what it means.

I read the JVM specification for the fps开发者_开发知识库trict modifier but still don't fully understand what it means.

Can anyone enlighten me?


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.


I like this answer, which hits the point:

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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号