Problem setting integer precision in Fortran

In summary, the conversation discusses a problem with compiling a program that uses big integers in Fortran. The error message indicates that the integer is too large for its kind. The solution is to use a larger integer kind, which can be achieved by setting the kind explicitly in the program.
  • #1
GabDX
11
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
  • #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.
 
  • #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:
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.
 
  • #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:
program prob003
   implicit none
   integer, parameter :: k32 = selected_int_kind(32)
   integer(kind=k32) :: num = 600851475143_k32 
end program prob003
 
  • #5
Yes, that worked. Thank you very much good sir!
 

Related to Problem setting integer precision in Fortran

What is the purpose of setting integer precision in Fortran?

The purpose of setting integer precision in Fortran is to specify the number of digits that can be stored and manipulated in integer variables. This allows for efficient use of memory and ensures accuracy in calculations.

How do I set integer precision in Fortran?

To set integer precision in Fortran, you can use the INTEGER(KIND=n) statement, where n represents the number of bits to be used for the integer variable. For example, INTEGER(KIND=4) sets the integer precision to 4 bytes or 32 bits.

What are the default integer precision settings in Fortran?

The default integer precision settings in Fortran may vary depending on the compiler and system, but it is typically set to 4 bytes or 32 bits. It is important to check the documentation of your specific Fortran compiler for the default settings.

Can I change the integer precision settings for specific variables in Fortran?

Yes, you can change the integer precision settings for specific variables in Fortran by using the INTEGER(KIND=n) statement before the variable declaration. This allows you to have different precision settings for different variables in your program.

Are there any limitations to setting integer precision in Fortran?

Yes, there are limitations to setting integer precision in Fortran. The maximum integer precision that can be set is dependent on the system and compiler, and it is important to check the documentation for the specific limitations. Additionally, setting a higher integer precision may result in slower performance and larger memory usage.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
3
Views
3K
  • Programming and Computer Science
Replies
8
Views
3K
  • Programming and Computer Science
Replies
2
Views
2K
Back
Top