Yotam
Yotam

Reputation: 10685

Two functions, different result. Suspected fortran feature

I am doing a Monte-Carlo (MC) simulation. I have a way to calculate the energy of the particle from scratch and a way to calculate only the difference between the old state and the new state. To test a modification to the code I have made, I calculated the energy after a step by the two way and I got different result. The number was low (of the scale of 10^-5 while the total energy is of the scale of 10^8) but I expected it to be even lower. There are three reasons I suspect this is a fortran issue

  1. Looking over all the interactions, I don't see any difference between the two ways

  2. The values repeat themselves

  3. Many of the values are negative powers of two (2^-14) for example.

I would appreciate your thoughts.

The said variable, and most variable used in the calculation (if not all) are of double precision type. The compiler is gnu f95.

Upvotes: 0

Views: 120

Answers (1)

M. S. B.
M. S. B.

Reputation: 29391

Double precision has about 15 to 17 decimal digits. Your two calculations have a difference of about a part in 10^13. Changing the order of calculations that for true real numbers would not change the answer can change the answer in finite precision arithmetic. Depending on how many calculations were made and details of your algorithm, a difference of this order could be plausible. A bug in a Fortran compiler seems unlikely ... if you remain convinced, try a different compiler.

Do you mean that you are using GNU gfortran? If so, which version?

Upvotes: 3

Related Questions