Migrating Fortran source code to Matlab precision issues
古いコメントを表示
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 日
So the result in t1 is then used in some way for the next calculation? In that case, unless the value is bit-for-bit identical to the one in Fortran, you will surely diverge sooner or later.
Can you post the Fortaran code and test scenario?
Have you tried writing it in C for a second opinion?
James Tursa
2012 年 5 月 18 日
Ditto. I would like to see the Fortran code and the MATLAB code side by side.
Chris
2012 年 5 月 18 日
Geoff
2012 年 5 月 18 日
After having googled myself a 5-second introduction to Fortran, can I ask what type the constant is? If the types are not the same, Fortran does implicit type conversion in much the same manner as C. You need to be careful to mimic this in every single calculation, as MatLab has its own type conversion that behaves a little differently.
Chris
2012 年 5 月 18 日
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 日
0 投票
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 件のコメント
Geoff
2012 年 5 月 18 日
I disagree on principle here =) If an algorithm is designed to use single precision, then it is not necessarily better to throw double precision at it. The way I read this question, it is a curiosity that using single precision in MatLab is giving different results to using single precision in Fortran.
However, I wonder about the integer conversion and whether there is overflow etc. That would be where we tread very carefully in MatLab because it does too much for us behind the scenes when mixing integers and reals.
Chris
2012 年 5 月 18 日
James Tursa
2012 年 5 月 18 日
Well, my 2 cents on this at this point:
- I don't know if the original algorithm was designed for single or not ... maybe it is really old Fortran code that used single for storage or throughput reasons and double precision would have been better from the start. OP doesn't really say one way or the other.
- If I think there is a precision issue then I would generally do what John suggests also, use higher precision variables and compare intermediate output to see what is going on.
- Without giving us any code to look at it is generally hopeless trying to help. The issue could be that the original algorithm is the one going wrong ... could be the MATLAB code is going wrong ... could be they are *both* wrong and need higher precision ... could be the conversion to MATLAB has typos in it or in some other way is not replicating the Fortran algorithm ... etc etc etc.
Without anything concrete to go on, the only thing I can suggest is OP prints a lot of intermediate output on both sides, compare them, and see what is going on and which one is right (if either).
BTW, just because the variables in Fortran are single precision doesn't mean the calculations are done in single precision ... on a PC it is likely that many intermediate calculations are done in 80-bit extended precision and then the result gets converted to single for storage.
Chris
2012 年 5 月 18 日
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.
カテゴリ
ヘルプ センター および File Exchange で Fortran with MATLAB についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!