Portability is one of the design goals of the Java language, and the same operation should get the same results regardless of which virtual machine is running. However, for the arithmetic operation of floating-point numbers, it is very difficult to achieve such portability. The double type uses 64 bits to store out a numeric value, while some processors use 80-bit floating-point registers, which increase the computational accuracy of intermediate processes. For example, the following calculations:
Double w = x * y/z;
Many INTEL processors compute x * Y and store the result in a register of 80, divided by Z and truncating the result to 64 bits, which results in a more accurate calculation and avoids exponential overflow. However, this result may not be the same as the result that is always calculated on a 64-bit machine.
The initial specification requires that all intermediate computations of a Java virtual machine must be truncated to ensure that floating-point computations, whether 80-bit or 64-bit, can maintain exactly the same results, but this may not only result in overflow but also more time consuming. To this end, Java acknowledges the conflicts between optimal performance and desirable results and gives improvements.
By default, Java virtual machines allow extended precision for intermediate calculations (if supported by the processor), but for methods marked with the STRICFP keyword, you must use strict floating-point calculations to generate reproducible results. If you can mark the main method as
public static strictfp void Main (string[] args)
In this way, all instructions in the main method will use a strict floating-point calculation. Or mark a class as STRICTFP, all methods in this class use strict floating-point calculations.
Above, from the "Java Core technology Volume I", slightly modified.
Summarize:
STRICTFP, or strict float point.
STRICTFP can modify a class or method so that it uses a strict floating-point calculation.
Strict floating-point calculations are not error-free without calculation results, and the opposite error may be greater, and may lead to overflow and loss performance.
If any rounding errors are not allowed in the numerical calculation, the BigDecimal class should be used.
Java floating Point arithmetic