Migrating Fortran source code to Matlab precision issues

2 views (last 30 days)
I have a piece of Fortran code that I have recently converted to Matlab. As I am going through and validating the translation, I'm running into an error with precision that is killing the performance of the Matlab code. It is an iterative code that repeats the same operations over many time steps, so the numerical differences that arise from the differences in precision end up building up and lead to wrong pieces of logic.
The issue right now is that in Fortran the single precision float variables XX and YY, are summed and being multiplied by a constant:
VAR = XX + YY
T1 = VAR*CONST
So T1 at this point is a float. The next line in Fortran casts the value of T1 to an integer:
IT1 = T1
In my Matlab scripts I accomplish this with the following:
var = xx +yy;
t1 = var*const;
it1 = fix(t1);
This works until about the 700th time step and the value of it1 in Matlab diverges from the one in Fortran. The value in Fortran of T1 = 1.0004226, fixing IT1 = 1, and in Matlab t1 = 0.99868, fixing it1 = 0;
My attempts at correcting this problem so far have been to single( ) all the values that go into the calculations of both XX and YY in Matlab. But this gets me to the 800th time step or so, and it falls apart again.
Does anyone have some words of wisdom that can help me with this problem?? I've been wracking my brain about this for the past couple of days and have come up with nothing.
  6 Comments
Chris
Chris on 18 May 2012
Both constants were declared as REAL = single precision floating point
Geoff
Geoff on 18 May 2012
The disparity between your two programs seems much more significant than just single precision error. I'd be looking for accidental rounding or incorrect typecasting elsewhere in the code.

Sign in to comment.

Answers (1)

John D'Errico
John D'Errico on 18 May 2012
So it sounds as if in Fortran, you are using SINGLE precision variables, and it is deviation from what you get in MATLAB, which will use double precision by default. The point is, the Fortran computation is probably the one going wrong, as it was done in lower precision.
If you want some clarity, use my HPF tool to verify which values are correct.
  5 Comments
Chris
Chris on 18 May 2012
Believe me I would like to be able to post up the code to get whatever help I can, but it's just not an option. It is really old Fortran code though.
I am doing a comparison check and printing the state vectors out of Fortran and comparing the results to Fortran is how I have been validating the progress of the simulation so far. There comes a point where the numerical differences in Matlab between the two variables I mentioned above, T1 in Fortran and t1 in Matlab, are
T1 = 1.0001 and t1 = 0.998
Upon fixing these two values to integers I get two different integers. Doing the following:
VAL = IT1*CONST2 !Fortran Code
val = it1*const2; %Matlab Code
produces two different numbers for val. Val is a state variable that is used to update other state variables.
That's interesting though about how intermediate calculations are performed. I'm working on RedHat Linux with running intel processors, and doing my compilation with GNU gfortran, and not a Windows PC ( I'm assuming you were referring to a Windows PC when you said PC)
John D'Errico
John D'Errico on 18 May 2012
The problem is, WE can't know what you have done wrong. Have you made a mistake in the re-write into MATLAB? Maybe. Is there a precision problem? Maybe. The only thing you can do (since you want show us the code, probably for a very good reason) is to check each computation. Write it out. Look at the intermediate results to see where the two diverge. My guess is that since this is the result of a long series of iterations when the two diverge, that tells me it is the accumulation of many tiny errors. That is what happens when you may be storing results in single precision, EVEN if they are done in higher precision registers internally. Eventually the loss of precision in the stored intermediate results causes a significant error. Again, only you will be able to verify this, since we cannot inspect what is probably a lengthy code, one where only you knows what it should be doing.

Sign in to comment.

Categories

Find more on Fortran with MATLAB in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!