Reputation: 126
I am programming a version of the BFGS algorithm for multidimensional optimization in Fortran 90. I have written the code, compiled it with gfortran on my laptop (running Windows Vista), and it works fine. But when I try to use the same code on a server running Linux 6, it gives incorrect results. It is the exact same code and it compiles fine, the results it produces are just wrong. Namely, it seems to be developing a floating point arithmetic error at some early phase of the algorithm and only produces one line of numerical results before it starts producing NaN's. I can only assume it's something about the server environment, but how can I find out what it is and how can I correct my code accordingly?
Upvotes: 0
Views: 346
Reputation: 1641
The problem is described here (CERT.org): FLP00-C. Understand the limitations of floating point numbers
The reason for this behavior is that Linux uses the internal extended precision mode of the x87 floating-point unit (FPU) on IA-32 machines for increased accuracy during computation. When the result is stored into memory by the assignment to c, the FPU automatically rounds the result to fit into a double. The value read back from memory now compares unequally to the internal representation, which has extended precision. Windows does not use the extended precision mode, so all computation is done with double precision, and there are no differences in precision between values stored in memory and those internal to the FPU. For GCC, compiling at optimization level 1 or higher eliminates the unnecessary store into memory, so all computation happens within the FPU with extended precision
The solution is described here: FLP02-C. Avoid using floating point numbers when precise computation is needed with two examples, an incorrect example showing the problem, and a corrected example without the problem:
"This code may be fixed by replacing the floating-point numbers with integers for the internal additions. Floats are used only when printing results and when doing the division to compute the mean."
Upvotes: 1