Nick Raines
Nick Raines

Reputation: 31

gfortran compiler Error: result of exponentiation exceeds the range of INTEGER(4)

I have this line in fortran and I'm getting the compiler error in the title. dFeV is a 1d array of reals.
dFeV(x)=R1*5**(15) * (a**2) * EXP(-(VmigFe)/kbt)
for the record, the variable names are inherited and not my fault. I think this is an issue with not having the memory space to compute the value on the right before I store it on the left as a real (which would have enough room), but I don't know how to allocate more space for that computation.

Upvotes: 3

Views: 1061

Answers (3)

zsalya
zsalya

Reputation: 454

Although there does seem to be an additional point here that may be specific to gfortran:

integer(kind = 8) :: result
result = 5**15
print *, result

gives: Error: Result of exponentiation at (1) exceeds the range of INTEGER(4) while

integer(kind = 8) :: result
result = 5**7 * 5**8
print *, result

gives: 30517578125

i.e. the exponentiation function seems to have an integer(4) limit even if the variable to which the answer is being assigned has a larger capacity.

Upvotes: 0

M. S. B.
M. S. B.

Reputation: 29401

As indicated in the comment, the value of 5**15 exceeds the range of 4-byte signed integers, which are the typical default integer type. So you need to instruct the compiler to use a larger type for these constants. This program example shows one method. The ISO_FORTRAN_ENV module provides the int64 type. UPDATE: corrected to what I meant, as pointed out in comments.

program test_program

use ISO_FORTRAN_ENV

implicit none

integer (int64) :: i

i = 5_int64 **15_int64

write (*, *) i

end program

Upvotes: 0

jack
jack

Reputation: 1790

The problem arises as one part of your computation is done using integer arithmetic of type integer(4). That type has an upper limit of 2^31-1 = 2147483647 whereas your intermediate result 5^15 = 30517578125 is slightly larger (thanks to @evets comment).

As pointed out in your question: you save the result in a real variable. Therefor, you could just compute that exponentiation using real data types: 5.0**15. Your formula will end up like the following

dFeV(x)= R1 * (5.0**15) * (a**2) * exp(-(VmigFe)/kbt)

Note that integer(4) need not be the same implementation for every processor (thanks @IanBush). Which just means that for some specific machines the upper limit might be different from 2^31-1 = 2147483647.

Upvotes: 3

Related Questions