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

Problem setting integer precision in Fortran

  1. May 19, 2013 #1
    I'm trying to work with big integers but for some reason this program won't compile:

    Code (Text):

    program prob003
       implicit none
       integer, parameter :: k32 = selected_int_kind(32)
       integer(kind=k32) :: num = 600851475143
    end program prob003
     
    The file name is prob003.f90 and I'm trying to compile with gfortran without any option:
    Code (Text):

    gfortran prob003.f90
     
    I keep getting this error:

    Code (Text):

    prob003.f90:3.42:

       integer(kind=k32) :: num = 600851475143
                                              1
    Error: Integer too big for its kind at (1). This check can be disabled with the option -fno-range-check
     
    It does compile if I remove 3 digits from that number however. What am I doing wrong?
     
  2. jcsd
  3. May 19, 2013 #2
    I'm not used to Fortran, but the error message just indicates that you can't fit such a large number into that kind of integer. Assuming 32 denotes the number of bits used to store the number, the largest number you can achieve is 2^31-1=2147483647, which is smaller than your number. Try a 64 bit integer instead, or some other supported larger integer.
     
  4. May 19, 2013 #3
    Thanks for the reply.

    I don't think that's the reason however. If I understand correctly, selected_int_kind(n) returns the number of bytes (not bits) needed to hold a number with n digits. For example, this code:

    Code (Text):

    program prob003
       implicit none
       integer, parameter :: k20 = selected_int_kind(20)
       integer(kind=k20) :: num
       print *, selected_int_kind(20)
       print *, huge(num)
    end program prob003
     
    prints the following:

    16
    170141183460469231731687303715884105727

    The function huge(num) returns the largest number that num can hold. With 16 bytes, that number is 256^16/2 - 1 which is precisely what is printed.
     
  5. May 19, 2013 #4
    Ah yes, I was definitely wrong. http://gcc.gnu.org/onlinedocs/gfortran/SELECTED_005fINT_005fKIND.html

    Based on this discussion http://gcc.1065356.n5.nabble.com/problem-with-integer-kind-td751850.html it seems that gfortran doesn't care about the type of the integer on lhs when it looks at the integer at the rhs, which it determines is too large to fit into the standard integer kind. You could try setting the kind explicitly in the rhs as well, as in

    Code (Text):

    program prob003
       implicit none
       integer, parameter :: k32 = selected_int_kind(32)
       integer(kind=k32) :: num = 600851475143_k32
    end program prob003
     
     
  6. May 19, 2013 #5
    Yes, that worked. Thank you very much good sir!
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: Problem setting integer precision in Fortran
  1. Setting precision? C++ (Replies: 5)

Loading...