Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Illegal Instruction Error in Mac terminal

  1. Sep 12, 2012 #1
    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


    ! 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
  2. jcsd
  3. Sep 12, 2012 #2
    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!
  4. Sep 12, 2012 #3


    Staff: Mentor

    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.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook