Problem setting integer precision in Fortran

Click For Summary

Discussion Overview

The discussion revolves around the issue of setting integer precision in Fortran, specifically when dealing with large integers that exceed the limits of the specified integer kind. Participants explore the implications of using different integer kinds and the behavior of the Fortran compiler in relation to integer size limitations.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant encounters a compilation error when trying to assign a large integer to a variable defined with a 32-bit integer kind.
  • Another participant suggests that the error arises because the number exceeds the maximum value for a 32-bit integer, proposing the use of a 64-bit integer instead.
  • A different participant challenges this view, explaining that the function selected_int_kind(n) returns the number of bytes needed to hold a number with n digits, not bits, and provides an example to illustrate this point.
  • One participant acknowledges their misunderstanding and cites a source indicating that gfortran evaluates the right-hand side integer independently of the left-hand side's kind, suggesting an explicit kind suffix for the integer on the right-hand side.
  • A later reply confirms that the proposed solution of using an explicit kind suffix resolves the compilation issue.

Areas of Agreement / Disagreement

Participants express differing views on the interpretation of the error message and the behavior of the Fortran compiler regarding integer kinds. While some agree on the need for a larger integer kind, there is no consensus on the initial understanding of selected_int_kind and its implications.

Contextual Notes

Limitations in understanding the behavior of integer kinds in Fortran are evident, particularly regarding the distinction between bits and bytes in the context of integer representation. The discussion also highlights the importance of specifying integer kinds explicitly to avoid compilation errors.

GabDX
Messages
11
Reaction score
0
I'm trying to work with big integers but for some reason this program won't compile:

Code:
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:
gfortran prob003.f90

I keep getting this error:

Code:
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?
 
Technology news on Phys.org
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.
 
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:
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.
 
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:
program prob003
   implicit none
   integer, parameter :: k32 = selected_int_kind(32)
   integer(kind=k32) :: num = 600851475143_k32 
end program prob003
 
Yes, that worked. Thank you very much good sir!
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 16 ·
Replies
16
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 3 ·
Replies
3
Views
5K