! From: "COMPUTATIONAL PHYSICS, 2nd Ed" ! by RH Landau, MJ Paez, and CC Bordeianu ! Copyright Wiley - VCH, 2007. ! Electronic Materials copyright: R Landau, Oregon State Univ, 2007; ! MJ Paez, Univ Antioquia, 2007; and CC Bordeianu, Univ Bucharest, 2007. ! Supported by the US National Science Foundation ! ! exp-good.f90: calculate e^ - x as a finite sum, good algorithm Program expgood Implicit none Real*8 :: element, min, max, step, sum, x Integer :: n min = 1E - 10 max = 10. step = 0.1 open(6, File = 'exp-good.dat', Status = 'Unknown') ! summation Do x = 0, max, step sum = 1 element = 1 n = 0 ! while loop may never stop Do while ((abs(element/sum) > min) .or. (sum .eq. 0)) n = n + 1 element = element*(-x)/n sum = sum + element End Do write (6, *) x, sum End Do close(6) Stop 'data saved in exp-good.dat' End Program expgood