Fortran Help - Do loops not calculating.

  • Context: Fortran 
  • Thread starter Thread starter skockler8
  • Start date Start date
  • Tags Tags
    Fortran Loops
Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Fortran90 code intended to approximate mathematical constants such as e, pi, and the Euler constant. Participants are addressing issues related to variable initialization, loop behavior, and output results.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant notes that the variable "n" needs to be defined before the first "DO WHILE" loop and points out that "n" is not being incremented within that loop.
  • Another participant suggests being cautious about mixing integer and real types, recommending the use of "real(i)" and "1./n_factorial" for better precision.
  • Concerns are raised about the potential size of "n_factorial," which could lead to very small values for "e" if it becomes too large, suggesting the use of DOUBLE instead of REAL.
  • A later reply indicates that the program compiles but does not produce outputs when run, suggesting that the program may be taking too long to execute.
  • Participants recommend adding print statements to identify where the program is getting stuck and to check the values of variables within the loops.

Areas of Agreement / Disagreement

Participants generally agree on the need to address variable initialization and type issues, but the discussion remains unresolved regarding the specific cause of the program's long execution time and lack of outputs.

Contextual Notes

There are limitations regarding the initialization of variables and the handling of data types, which may affect the program's performance and output. The exact nature of the loops and their conditions remains a point of uncertainty.

skockler8
Messages
3
Reaction score
0
Hello all,

I am working on a Fortran90 code that will give me an approximation of a few mathematical constants. My code will compile and run, however, my outputs are given as 0.000000 for all constants that I am calculating in the loops. Any help would be greatly appreciated. My code is as follows:

PROGRAM HomeWorkOne_ProbOne

IMPLICIT NONE

! Computes an estimation of the exponential function, pi, and the Euler Constant

REAL :: e, EC, pi, n_factorial, x_factorial, gamma
INTEGER :: i, n, x


x = n + 3.0/2.0

n_factorial = 1.0


DO WHILE (n < 10)
DO i = 1, n
n_factorial = n_factorial * i
END DO
e = 1/n_factorial
END DO

DO WHILE (n < 10)
EC = (1.0/n) - (LOG(REAL(n)+1.0)-LOG(REAL(n)))
END DO

DO WHILE (x < 10)
DO i = 1, x
x_factorial = x * i
gamma = (2.0 * x_factorial)/(2.0 * n + 3.0)
END DO
DO i = 1, n
n_factorial = n_factorial * i
END DO
pi = (n_factorial)/((2**(n-(1.0/4.0)*Cos(3.14*(2.0*n+1.0))))+(3.0/4.0)*(3.14**((1.0/4.0)*Cos(3.14*(2*n+1))-(1.0/4.0)))*gamma)
END DO

PRINT *, "Approximate values of:"
PRINT *, "e =", e
PRINT *, "gamma =", EC
PRINT *, "Pi =", pi
PRINT *, "Actual values of :"
PRINT *, "e =", 2.71828182
PRINT *, "gamma =", 0.57721566
PRINT *, "Pi =", 3.14159265

END PROGRAM HomeWorkOne_ProbOne


Thanks for any help!
 
Technology news on Phys.org
Hello.

You need to define "n" before "DO WHILE (n < 10)".
Also, n is not incremented in that loop.

Be careful not to mix integers and reals. For example, "n_factorial * i" is a real times an integer. "real(i)" will be better. The same for 1/n_factorial (try "1./n_factorial")

Finally, check the value of n_factorial. If it's really large, then e will be really small and you might have to use DOUBLE instead of REAL.

Hope this helps.

Tom
 
Last edited:
I have done all that, it has helped greatly. Now, the program compiles completely, however, when I run it, it takes a long time and I don't receive any outputs.

Thanks
 
Great.
Try putting in some lines like
print *, "a"
print *, "b"
etc

after each part of the code to see where it's getting stuck. Then when you know which loop is causing the problem, print out important numbers like
print *, "n= ", n
inside the loop. This should allow you to determine where it's getting stuck. Perhaps you're not incrementing one of your variables?
 
Welcome to Physics Forums, skockler8!

I guess you didn't notice that we have a programming forum. I've moved this thread there.
 

Similar threads

  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 19 ·
Replies
19
Views
3K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 2 ·
Replies
2
Views
8K