Problem setting integer precision in Fortran

Click For Summary
SUMMARY

The discussion focuses on compiling a Fortran program that handles large integers using the gfortran compiler. The user encounters an error due to the integer exceeding the maximum limit for a 32-bit integer, which is 2^31-1 (2147483647). The solution involves using a 64-bit integer by explicitly specifying the kind in the assignment, as demonstrated with the syntax `600851475143_k32`. This adjustment resolves the compilation issue.

PREREQUISITES
  • Understanding of Fortran programming language syntax
  • Familiarity with gfortran compiler options
  • Knowledge of integer kinds in Fortran, specifically selected_int_kind
  • Concept of integer limits and precision in programming
NEXT STEPS
  • Explore Fortran's integer kinds and their limits using selected_int_kind
  • Learn about the implications of using different integer types in Fortran
  • Research gfortran compiler options for handling integer range checks
  • Investigate the use of large integer libraries in Fortran for enhanced precision
USEFUL FOR

This discussion is beneficial for Fortran developers, particularly those working with large numerical computations, as well as educators teaching Fortran programming concepts related to data types and precision.

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
2K
  • · 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