Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Fortran's precision vs. MATLAB's 1

Status
Not open for further replies.

NickFort

Technical User
Jun 10, 2010
113
I'm trying to translate a piece of code from MATLAB to Fortran (one that typically has lots of iterations), line-by-line. To check that everything is fine and dandy, in both, I output all of the variables at each assignment (an arduous process!). What I've found is that as the iterations proceed, the Fortran code deviates further and further from the MATLAB, by very small amounts that accumulate to something noticeable by the end of the iterations.

Both use double precision, yet, somehow, MATLAB seems to maintain its accuracy while Fortran loses it. I think that the two somehow handle the finite nature of machine numbers differently.

The errors (i.e. difference between MATLAB and Fortran) start off somewhere around 0.000001%, and end off at about 0.08%. One variable even peaks at about 3% difference. This, unfortunately, seems to have an effect on the stability of the whole calculation.

Any idea why there is this discrepancy, and -- more importantly -- do you have any suggestions for mitigating this? Any strategies would be helpful!

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
It is very easy to loose precision, for instance in using single precision real values in mathematical expressions.
So, at your place, I would carefully verify the real values like 1. which must be written 1.d0 instead.

François Jacq
 
Thanks for the reply, FJacq. I was convinced that this wasn't the case, because I've been very careful with this sort of thing, and had checked the main code, but as it turns out, some loops from an old piece of code didn't initialise properly with double precision.

Thanks for the suggestion!

--------------------------------------
Background: Chemical engineer, familiar mostly with MATLAB, but now branching out into real programming.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top