Nils_M
Nils_M

Reputation: 1070

Fortran/C Interlanguage problems: results differ in the 14th digit

I have to use C and Fortran together to do some simulations. In their course I use the same memory in both programming language parts, by defining a pointer in C to access memory allocated by Fortran. The datatype of the problematic variable is

real(kind=8)

for Fortran, and

double

for C. The results of the same calculations now differ in the respective programming languages, and I need to directly compare them and get a zero. All calculations are done only with the above accuracies. The difference is always in the 13-14th digit.

What would be a good way to resolve this? Any compiler-flags? Just cut-off after some digits?

Many thanks!

Upvotes: 2

Views: 286

Answers (5)

caf
caf

Reputation: 239051

Since you are compiling for an x86 architecture, it's likely that one of the compilers is maintaining intermediate values in floating point registers, which are 80 bits as opposed to the 64 bits of a C double.

For GCC, you can supply the -ffloat-store option to inhibit this optimisation. You may also need to change the code to explicitly store some intermediate results in double variables. Some experimentation is likely in order.

Upvotes: 1

M. S. B.
M. S. B.

Reputation: 29391

Perfect portability is very difficult to achieve in floating point operations. Changing the order of the machine instructions might change the rounding. One compiler might keep values in registers, while another copy it to memory, which can change the precision. Currently the Fortran and C languages allow a certain amount of latitude. The IEEE module of Fortran 2008, when implemented, will allow requiring more specific and therefore more portable floating point computations.

Upvotes: 1

Mark Wilkins
Mark Wilkins

Reputation: 41222

One thing you might check is to be sure that the FPU control word is the same in both cases. If it is set to 53-bit precision in one case and 64-bit in the other, it would likely produce different results. You can use the instructions fstcw and fldcw to read and load the control word value. Nonetheless, as others have mentioned, you should not depend on the accuracy being identical even if you can make it work in one situation.

Upvotes: 2

Thomas
Thomas

Reputation: 181785

Floating point is not perfectly accurate. Ever. Even cos(x) == cos(y) can be false if x == y.

So when doing your comparisons, take this into account, and allow the values to differ by some small epsilon value.

Upvotes: 2

Femaref
Femaref

Reputation: 61437

This is a problem with the inaccuracy with floating point numbers - they will be inaccurate and a certain place. You usually compare them either by rounding them to a digit that you know will be in the accurate area, or by providing an epsilon of appropiate value (small enough to not impact further calculations, and big enough to take care of the inaccuracy while comparing).

Upvotes: 2

Related Questions