2

I am setting an integer to a value less than its maximum, but receiving an error that it is too big for it's kind. Why is this? Here is a sample program.

program max_int

integer, parameter :: i32 = selected_int_kind(32)

integer(kind = i32) :: my_int

!The largest integer of this kind
print*, huge(my_int)

!This works
my_int = 100000

!This doesn't, and gives an error.
!my_int = 1000000000000

print*, my_int

end program
astromonerd
  • 907
  • 1
  • 15
  • 32
  • 3
    Just wanted to point out that `selected_int_kind` uses powers of 10, not 2, so you probably meant `selected_int_kind(13)` in order to resolve this number (which will also need to be 64 bit). – marshall.ward Sep 19 '13 at 00:46
  • Similar question: http://stackoverflow.com/questions/3204616/long-ints-in-fortran – M. S. B. Sep 19 '13 at 10:12
  • 1
    i guess we are doomed to keep answering this so long as gfortran keeps producing that vague error message! – agentp Sep 19 '13 at 16:58

1 Answers1

7

You need to append the i32 to 1000000000000 like this: 1000000000000_i32. gfortran complains that the number is too big for it's kind. 1000000000000 is a short integer, not a super long integer. 1000000000000_i32 is a super long integer. It is not complaining about the variable, it's complaining about the constant.

dwwork
  • 838
  • 5
  • 12
  • This works. But I have this integer type all over my code, to accommodate numbers ranging from 1 to this big number. Why will the code compile when I set this type of int to a smaller value, but I need the append for a value like the above? – astromonerd Sep 19 '13 at 14:41
  • @jgoldst1 The compiler assigns a `kind` to each constant and each variable in the code. The compiler will do the conversion from a default kind (processor dependent) to the user specified `i32` kind as long as the number falls inside the range (and precision) of the default kind. `1000000000000` as a default kind does not fit inside the range of the default kind, as so you have to force the compiler to recognize it as a `i32` kind. – dwwork Sep 19 '13 at 15:41