Migrating Fortran source code to Matlab precision issues
2 次查看(过去 30 天)
显示 更早的评论
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 个评论
Geoff
2012-5-18
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.
回答(1 个)
John D'Errico
2012-5-18
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 个评论
John D'Errico
2012-5-18
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Fortran with MATLAB 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!