Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
29352 Discussions

Issue with order of mathematical operations

fogla
Beginner
743 Views
I am running two different FORTRAN codes which are exactly the same, except for the small difference that in one of the codes there is a term evaluated as 4*(pi*pi)*k*k and the other code has the same term evaluated as 4*pi*pi*k*k (where pi and k are double precision variables with the exact same values upto the last decimal). The two expressions give results which differ from each other in the 16th decimal place. Though this is a very minor issue, the PDE which I am trying to solve is so sensitive to numerical noise, that a small change such as the one mentioned above can cause a significant change in the solution. Could anyone explain how does the location of brackets in an expression affect the result of that expression?
0 Kudos
3 Replies
Steven_L_Intel1
Employee
743 Views
Try adding either "-assume protect_parens" or "-fp_model source".

A compiler ought not to reorder operations across parentheses but ifort will do so with its default modes.
0 Kudos
TimP
Honored Contributor III
743 Views
Try adding either "-assume protect_parens" or "-fp_model source".

A compiler ought not to reorder operations across parentheses but ifort will do so with its default modes.
In addition to setting one of those options, you would need to use parentheses sufficient to dictate the equivalent order of evaluation in both cases, if that is your goal, e.g. 4*(pi*pi)*(k*k).
0 Kudos
jimdempseyatthecove
Honored Contributor III
743 Views

You might want to consider altering your method in a manner that reduces sensitivity to numerical noise. There is a difference between getting consistent results and getting better results.

For example, we know pi (assuming pi above is 3.141...) has a fraction part that extends beyond the precision of a REAL(8). We know that 4 can be expressed exactly. The k may or may not have an exact representation.

Knowing this, you might find better approximations using 4*((pi*k)**2), with option to honor parens.

Someone here may have a suggestion on a book with programming techniques that preserve more of the accuracy of your calculations by means of re-ordering steps within a program.

Jim Dempsey


0 Kudos
Reply