Illegal Instruction Error in Mac terminal

Click For Summary
SUMMARY

The discussion addresses an "Illegal Instruction" error encountered while executing a Fortran program on Mac OS X 10.7. The user’s code attempts to compute approximations for the exponential function, pi, and the Euler constant but fails due to potential issues with array bounds and data type mixing. Key insights include the need to ensure that array sizes match the intended calculations and the importance of using debugging techniques such as STOP commands to isolate errors. The problem is likely related to the DF array, which is declared with 102 elements but accessed beyond its bounds.

PREREQUISITES
  • Understanding of Fortran programming language
  • Familiarity with array indexing and bounds in programming
  • Knowledge of debugging techniques in coding
  • Basic concepts of numerical approximation methods
NEXT STEPS
  • Review Fortran array handling and bounds checking
  • Learn about debugging techniques in Fortran, including the use of STOP and PRINT commands
  • Explore numerical methods for approximating mathematical constants
  • Investigate data type compatibility and conversions in Fortran
USEFUL FOR

Fortran developers, programmers debugging numerical computations, and anyone interested in improving their coding practices in scientific programming.

skockler8
Messages
3
Reaction score
0
I am running a mac os x 10.7 system I am writing a fortran code which compiles, however, whenever I run my code (./file.out) all I receive in return is the message "Illegal Instruction". I have tried messing with my code and I continue to receive this message. Any help with this would be great. My code is as follows:

!
PROGRAM HomeWorkOne_ProbOne

IMPLICIT NONE

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

! Declare all variables
INTEGER(8) :: n
INTEGER, PARAMETER :: max = 100
REAL(8), DIMENSION (max + 2) :: f, DF, p, pi, gamma, e

! Set initial values of variables to approximate
pi = 0.0
gamma = 0.0
e = 0.0

! Calculation of n factorial set equal to f(n)
DO n=1, max
f(1) = 1.0
f(n+1) = f(n)*n
END DO

! Calculation of (2n+1)! set equal to DF(n)
DO n=1, (2*max+1)
DF(1) = 1.0
DF(2) = 1.0
DF(n+2) = DF(n+1)*(2*n+1)
END DO

! Calculation of F/DF set equal to p(n)
DO n=0, max
p(n+1) = f(n+1)/DF(n+1)
END DO

! Loop that will calculate approximation values
DO n=1, max
e(n+2) = (1.0/f(n+1)) + e(n+1)
gamma(n+2) = gamma(n+1) + ((1.0/n)-LOG((n+1.0)/n))
pi(n+2) = p(n+1) + pi(n+1)
END DO

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

END PROGRAM HomeWorkOne_ProbOne
 
Technology news on Phys.org
My guesses would either be that you're going beyond the allocated range of your arrays (DF seems a possible candidate) or that you're dividing by zero or taking the log of a non-positive number.

Also, you're still mixing integers and reals (eg, at "1.0/n", etc) which might cause values to be other than you expect.

Try putting STOP commands in your program or temporarily deleting parts of your program until you find which part is causing the error.

When debugging, breaking the code down into small bits and using STOP and PRINT commands is a really useful way to check it - it is the only way, really.

Good luck!
 
I agree that there's a problem with DF. You have declared it to be an array with 102 elements, but your loop is attempting to store values in 201 elements, since n in that particular loop ranges from 1 to 201.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 15 ·
Replies
15
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 6 ·
Replies
6
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K