What is the purpose and where should I use "strictfp"

2

I was reading the Java 9 specification and I came across the keyword strictfp , l for what it's just that was not clear.

What good is this and where should I apply?

Why is not it so used in programs today?

Why was not it removed (if not used)?

    
asked by anonymous 21.12.2018 / 21:12

1 answer

2

There is a technical standard called IEEE 754 , which defines how the representation of the double-floating point precision. In Java, this representation is materialized in the data type double . In other words, the representation of this technical standard is followed strictly when working with double in Java.

As long as the JVM controls and performs operations involving double , it is guaranteed that double precision occurs the way it should occur (following the IEEE 754 standard). However, for optimization and speed issues, it can happen that the JVM delegates these operations to the machine's processor and that is where things can get complicated because, as we know, there are several types of processors in the market, each with its architecture and features including how they deal with calculations involving floating points and do not necessarily follow the technical standard cited above. This means that there is no guarantee that the same calculation, involving the same numbers, will always generate the same precision on different processors (that is, the result is not deterministic) and this can be a problem if you, for example, have a program of mathematical application that needs extreme precision.

To ensure that a calculation with these numbers is always performed by the JVM and not delegated to the processor (thus eliminating the possibility of different results depending on the processor), the keyword will be used in the variables that will be used in this calculation strictfp . This ensures that, regardless of the platform on which your program runs, a floating-point calculation will be deterministic (given the same input data, the result will always be the same).

For 99.9% of developers, however, these possible differences do not matter, since most applications developed by them do not require such precision, and this explains why you hardly ever come across that keyword being used, but make no mistake, it is used rather, probably in scientific applications such as particle accelerators or even mathematical modeling.

    
22.12.2018 / 02:45