2

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.

Jonas
  • 121,568
  • 97
  • 310
  • 388
  • 1
    What part of the error message do you not understand? `5` is an integer. `5**(15)` is an integer raised to integer power. (BTW, the parentheses are unneeded). The largest default integer on your system is likely 2147483647. 5**15 = 30517578125, which is slightly larger than the largest default integer value. – evets Nov 17 '20 at 02:55

3 Answers3

2

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.

jack
  • 1,658
  • 1
  • 7
  • 18
  • Nitpicking - I would say "of type integer, kind 4. For most processors this kind of integer, if it is implemented, has an upper limit ..." – Ian Bush Nov 17 '20 at 09:11
0

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
M. S. B.
  • 28,968
  • 2
  • 46
  • 73
  • `5_real64` rather than `5._real64` or `5_int64` would seem to be somewhat less than recommended. But either way, I'd have thought we'd just need to have `5.**15` instead of `5**15` as it's likely the expression of the question is a real one? – francescalus Nov 17 '20 at 05:52
  • 1
    5_real64 is still an integer constant. This answer should be deleted. – evets Nov 17 '20 at 06:51
  • two problems here. (1) as @evets pointed out `5_real64` is still an integer. that is just misleading. (2) why would you compute the exponentiation using `real64` but save the result in an `int64` type? the whole computation should just be done using floating point values.. – jack Nov 17 '20 at 08:44
0

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.

zsalya
  • 454
  • 4
  • 8
  • 1
    The left-hand side of intrinsic assignment never affects the evaluation of the right-hand side. – francescalus Jan 11 '22 at 20:39
  • There is no exponentiation in `5*15`. Please check that your code is what you actually compiled and tested. Also, read https://stackoverflow.com/questions/3170239/fortran-integer4-vs-integer4-vs-integerkind-4 to see why using just `8` in `integer(kind = 8)` is ugly and not portable. – Vladimir F Героям слава Jan 13 '22 at 17:46