Fortran Help - Do loops not calculating.

In summary: Thanks for the input.In summary, the code is taking a long time to run and it doesn't produce any outputs.
  • #1
skockler8
3
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
  • #2
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:
  • #3
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
 
  • #4
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?
 
  • #5
Welcome to Physics Forums, skockler8!

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

Related to Fortran Help - Do loops not calculating.

1. Why is my do loop not calculating?

There could be several reasons why your do loop is not calculating. First, make sure that you have correctly initialized your loop counter and have specified the correct range of values for the loop. Also, check for any logical or syntax errors in your code. Additionally, make sure that you are using the correct arithmetic operators and have included the necessary statements to perform the calculations within the loop.

2. How can I troubleshoot my do loop not calculating?

To troubleshoot your do loop, you can use debugging tools such as print statements or a debugger program. These tools can help you identify any errors or issues within your code that may be causing the loop to not calculate. You can also try breaking down your code into smaller sections and testing each one individually to isolate the problem.

3. Are there any common mistakes that can cause a do loop to not calculate?

Yes, some common mistakes that can cause a do loop to not calculate include using incorrect syntax, not properly initializing the loop counter, forgetting to include necessary statements within the loop, and using the wrong arithmetic operators. It is also important to make sure that the loop is iterating through the correct range of values and that any necessary conditions are being met within the loop.

4. Can using the wrong data types affect the calculation within a do loop?

Yes, using the wrong data types can definitely affect the calculation within a do loop. Make sure that you are using data types that are compatible with the arithmetic operations you are performing within the loop. If you are unsure, you can refer to the documentation for your specific Fortran compiler to see which data types are supported.

5. How can I optimize my do loop for better performance?

To optimize your do loop for better performance, you can try reducing the number of iterations, optimizing the loop body by using efficient algorithms, and avoiding unnecessary calculations within the loop. You can also consider using parallel processing techniques if your Fortran compiler supports them. It is important to carefully analyze your code and make strategic changes to improve the performance of your do loop.

Similar threads

  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
4
Views
644
  • Programming and Computer Science
Replies
5
Views
3K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
6
Views
1K
  • Programming and Computer Science
Replies
12
Views
974
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
4
Views
12K
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
19
Views
2K
Back
Top