Fortran Problem setting integer precision in Fortran

AI Thread Summary
The discussion revolves around a Fortran program that fails to compile due to an integer overflow error. The user attempts to define a large integer (600851475143) using a 32-bit integer kind, which exceeds the maximum value for that type (2^31-1). The error message indicates that the integer is too large for its kind. A suggestion is made to use a 64-bit integer instead, as the selected integer kind function returns the number of bytes needed to store a number, not bits. The user learns that gfortran evaluates the right-hand side integer independently of the left-hand side's kind, leading to the overflow error. A solution is found by explicitly specifying the kind for the right-hand side integer, allowing the program to compile successfully.
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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.

Similar threads

Replies
4
Views
2K
Replies
5
Views
2K
Replies
16
Views
2K
Replies
4
Views
2K
Replies
3
Views
3K
Replies
3
Views
5K
Back
Top