Help to a FORTRAN newbie with KIND, please

  • Fortran
  • Thread starter balthamossa2b
  • Start date
  • Tags
In summary, the KIND keyword applies to REAL variables only, and integer variables can be specified with the following sizes: INTEGER*1, INTEGER*2, INTEGER*4, INTEGER*8.
  • #1
Hi, I'm starting with FORTRAN (I have Microsoft Powerstation) and I was experimenting with KIND. Specifically, what I wanted was:

integer (kind=8):: list of variables

But the compiler doesn't let me use KIND with integers if kind>4, and I really need to use long integers. Can someone point out what I'm doing wrong, please?

Thanks in advance.
Technology news on
  • #2
I don't know this for a fact, but I believe the KIND keyword applies only to REAL variables, and the choices are 1, 2, and 3. These represent 4-byte, 8-byte, and 10-byte reals.

For integers, I believe you can specify the size of storage as INTEGER*1, INTEGER*2, INTEGER*4, and INTEGER*8. You'll need to look at your compiler documentation to get a definitive answer.
  • #3
In a large CFD code that I use, we have a module that looks like this:
MODULE Precision_Def


  PUBLIC :: i_def,      r_def,      & ! precision for calculation
            i_def_sp,   r_def_sp,   & ! single precision (4 byte)
            i_def_dp,   r_def_dp,   & ! double precision (8 byte)
            intDef,     realDef

                        r_def    = SELECTED_REAL_KIND(12), &
                        i_def_sp = SELECTED_INT_KIND(9),   &
                        r_def_sp = SELECTED_REAL_KIND(6),  &
                        i_def_dp = SELECTED_INT_KIND(16),  &
                        r_def_dp = SELECTED_REAL_KIND(12), &
                        intDef   = i_def,                  &
                        realDef  = r_def

END MODULE Precision_Def
Then, when we creat variables in subroutines, etc, they look like this:
INTEGER(KIND=i_def) :: ...
REAL(KIND=r_def) :: ...
Again as mentioned, it amy be compiler specific, although we've compiled and ran using both g95 and ifort.
  • #4
KIND values are compiler specific (which seems pretty silly but that is what the standard says). minger's answer seems like the way to go -- although I quite like the real*8 and integer*8 type declarations.
  • #5
The nice thing about a module defining it, is e.g. if you decide you want to port over to a 64-bit machine and want to run a higher precision, you can just change the one module, rather than every single subroutine where variables are declared.

1. What is KIND in FORTRAN?

KIND is a keyword used in FORTRAN to specify the data type and precision of a variable. It helps to define how much memory will be allocated for the variable and how the data will be stored in that memory.

2. How do I use KIND in FORTRAN?

To use KIND in FORTRAN, you need to declare the KIND type for the variable in the declaration statement. For example, "INTEGER(KIND=4) :: num" will declare an integer variable named "num" with 4 bytes of memory allocated.

3. What are the different kinds of data types in FORTRAN?

There are four main data types in FORTRAN: INTEGER, REAL, COMPLEX, and CHARACTER. Each of these types has different KIND values that can be specified to define the precision and range of the data.

4. How do I choose the right KIND for my variable?

The right KIND for your variable depends on the range and precision of the data you want to store. For example, if you need to store large numbers, you may need to use a larger KIND value for your INTEGER variable. It is important to choose the appropriate KIND to avoid overflow or loss of precision in your calculations.

5. Can I change the KIND of a variable in FORTRAN?

No, once a variable is declared with a specific KIND, it cannot be changed. If you need to change the precision or range of a variable, you will need to declare a new variable with the desired KIND and assign the value of the old variable to it.