TCW
TCW

Reputation: 25

Expontentiation is not working as I expect

I am new to Fortran. I am trying to compute a simple function, but I'm having trouble with the exponent. If I run the following:

module procedures

    contains

        double precision function f(x)
             f = x**sigma - alpha*x**sigma + 5
             return
        end function f   

end module procedures

program main
    use procedures

    parameter (alpha = 5.0, sigma = 2)
    write(6,"('f(3) = ',1f15.8)") f(3.0)

end program main

I get f(3) = 6, even though the answer should be -31. I feel like I'm missing something extremely basic about exponentiation.

Upvotes: 0

Views: 104

Answers (1)

Dan Sp.
Dan Sp.

Reputation: 1447

Careful with your modules. You defined alpha and sigma in the main program but not in the module therefore, alpha and sigma are equal to zero in the module, then: x** 0 - 0*x** 0 + 5 = 6 for any value of x.

You do not need another module for this. Define the parameters inside the module above. Since the main program uses this module, the values are valid in the module, in the main program, and any other program segment that has 'use procedures' in it.

Upvotes: 1

Related Questions