Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

SQRT() function returning integer using gfortran mac osx SL

Status
Not open for further replies.

ChadGarwick

Technical User
May 25, 2010
4
US
Thank you for taking the time to read this. I am currently going through the text "Classical FORTRAN" and I am having some difficulty understanding a bug. Reading through the text, I am led to believe the the SQRT() function should return a REAL if I pass it a REAL. When I compile this code and I get an integer:
Code:
      A=SQRT(8.0)
      PRINT *,A
      STOP
      END
I compile using gfortran in this way:

>gfortran test.for

The output is not what I would expect. I have a number of spaces, and the integer 2 as output:
Code:
                2
Am I missing a compiler flag or something? I was under the impression that gfortran should compile f77 without any problems. Is it possible that the gfortran binaries that I downloaded are bad? Any advice would be appreciated. My google searches are not turning up a solution to this problem. I have gone through the text a number of times and can not find any resolution to this issue.

Thank you again for your time.
 
Well, I solved my own problem. I have programmed in Python, and a little C++, for years. I never in all my days of programming had assumed that the first character in a variable name would have ANY bearing on the type of data it could hold. I was wrong! Apparently if the first character of the variable starts with an I,J,K,M, or N the variable is assumed to be an integer. I was playing with the program, and found that the program worked just fine if I did not start the variable name with an I (like I had been doing in the program I was working on.)

Problem has been solved. Apparently declaring the variables one uses BEFORE using the variable will alleviate any future problems of this sort. If someone stumbles upon the issue in the future, I hope this explanation helps.

Cheers
 
If you try this (without variable declaration)
Code:
      I=SQRT(8.0)
      PRINT *,I
      STOP
      END
you will get as result 2.

But when you declare your variable first
Code:
      REAL I
      I=SQRT(8.0)
      PRINT *,I
      STOP
      END
you will get the result 2.8284271




 
Mikrom,

Thank you for the reply. That is indeed what was going on in my "Quadratic Equation" program (advanced stuff ehh?) I had an "IMGPRT" or imaginary part and "REALPT" or Real part. I will ensure that I declare variables before I use them from now on.

Cheers
 
ChadGarwick said:
I will ensure that I declare variables before I use them from now on.
Then use IMPLICIT NONE. This forces the variable declaration.
For example with this
Code:
      IMPLICIT NONE
      I=SQRT(8.0)
      PRINT *,I
      STOP
      END
the compiler generates Error: Symbol 'i' at (1) has no IMPLICIT type

Now, when you want to compile your source, then you need to declare all variables in your program:
Code:
      IMPLICIT NONE
      REAL I
      I=SQRT(8.0)
      PRINT *,I
      STOP
      END
 
mikrom,

IMPLICIT NONE will be added to all of my programs. One of the biggest challenges when switching from C++ to Python was wrapping my head around dynamic typing. Since all of my scientific coding has switched to Python, I have learned to accept (and guiltily love) this feature of the language. Switching gears back to declaring variable types should be fun. Thank you again for all of your help. Back to the grind stone. I get to play with these pesky arrays. I hope they are as easy to manipulate as numpy arrays in python.

Cheers
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top