0

I am trying to do the following simple assignment in Fortran to a 8 byte integer:

integer(kind=8)    :: a
a = 30 * 1000000000
print *,a

However, when I print the value I get the following output:

-64771072

Which seems like either the print or the calculation 30 * 1000000000 is being done with 4 bytes.

  • Is the problem located in the assignment of the variable or in the print -or elsewhere-?
  • How can I fix it?
  • Is it dependent on a specific Fortran standard?

I am using flang in a 64 bits FreeBSD system in case it is relevant.

Edit:

As per comments suggestion I have downloaded kindfind.f90 and executed it. This is the output. My understanding is that in my compiler, kind=8 shall provide a 8 bytes as per program description (see here).

$ ./a.out 



 !---------------------------------------------------------!
 !                                                         !
 !                    KINDFINDER                           !
 !                                                         !
 !            KIND Search & Evaluation for                 !
 !          FloatingPoint and Integer Types                !
 !           of the local Fortran Compiler                 !
 !                                                         !
 ! Author: Werner W Schulz (C) 1998                        !
 ! (email: wws20@cam.ac.uk)                                !
 !                                                         !
 !                                                         !
 ! FloatingPoint Model Parameters:                         !
 ! Kind   Precision       Range          Name              !
 !    4           6          37          Single            !
 !    8          15         307          Double            !
 !                                                         !
 !                                                         !
 ! Integer Model Parameters:                               !
 ! Kind   Range                          Name              !
 !    1       2                          Int_01            !
 !    2       4                          Int_02            !
 !    4       9                         Int_Def            !
 !    8      18                          Int_08            !
 !                                                         !
 !                                                         !
 !                                                         !
 ! NOTE:                                                   !
 ! KindFinder has generated a Programme and a Module in    !
 !                                                         !
 !     NumericModel.f90                                    !
 !     Fortran_Kind_Module.f90                             !
 !                                                         !
 ! that will compute and display the Numerical Parameters  !
 ! of the various INTEGER & FLOATING POINT Types           !
 ! as specified by Fortran and the local Compiler.         !
 ! The ModuleFile is can be used elsewhere.                !
 !                                                         !
 ! All Code is compatible with the Fortran Subsets and     !
 ! should be able to run on any Fortran90-conforming       !
 ! Compiler including F and Elf90.                         !
 !                                                         !
 ! NOTE:                                                   !
 ! Possible KIND Values for LOGICAL or CHARACTER Types     !
 ! must be obtained in the local Fortran Reference Manual  !
 !                                                         !
 !---------------------------------------------------------!



Warning: ieee_inexact is signaling
FORTRAN STOP
$ 
M.E.
  • 4,955
  • 4
  • 49
  • 128
  • 3
    Just off to bed but kind=8 is not guaranteed to be an 8 byte integer, or to be supported by your compiler. Use selected_int_kind or iso_fortran_env to portably obtain a suitable kind. See https://stackoverflow.com/questions/838310/fortran-90-kind-parameter for how it works for reals. Integers work analogously. Then to get an expression with integers with eh appropriate range just use the appropriate kind throughout, constants included. – Ian Bush Jun 01 '21 at 22:39
  • I will add the output from `kindfind.f90` to the original question. – M.E. Jun 01 '21 at 22:43
  • 1
    @M.E. Yes, we know it is that way, else the compiler would refuse the program earlier. But the point is that you should not be using those values like 4 or 8 directly. The next person using your code might use a different compiler. Even a compiler that does not exist yet. – Vladimir F Героям слава Jun 02 '21 at 06:11
  • The answers provided in the duplicate question help to understand how to handle this. – M.E. Jun 02 '21 at 07:15

0 Answers0