Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

[Fortran] Integer overflow for a real number?

  1. Jun 17, 2015 #1
    I am trying to define a real number as follows:
    Code (Fortran):
    real*8 r
    r=2.5*10**20
    This results in the following warning:
    r=2.5*10**20
    1
    Warning(131): Integer overflow at 1​

    First off, I am not even declaring it as an integer, yet the warning is named as such.
    After searching around for similar errors, and consulting the g95 manual, I decided to try out the KIND setting:
    Code (Fortran):
    real(kind=10) r...
    considering that (from the g95 manual):
    "REAL(KIND=10) for x86-compatible systems. 19 digits of precision, value range 10^±4931"​
    Since 20 is clearly less than 4931, I figured that would do it. It did not. I tried bumping the kind as high as the compiler would take it (16) and it still did not clear the warning. 16 even seems to be the appropriate choice of KIND, since
    Code (Fortran):
    print*,selected_real_kind(20)
    returns a value of 16.

    I am at a loss for how to resolve this issue. It is strange though, my real*8 variables that are on the order of 10**(-30) work without issue. Any help would be appreciated. Thanks!
     
  2. jcsd
  3. Jun 17, 2015 #2

    Mark44

    Staff: Mentor

    This should be written as
    Code (Text):
    r = 2.5E20
    Apparently the compiler is calculating 10**20 as in integer value, before multiplying by 2.5. This causes an overflow, assuming an integer is signed four bytes (maximum size is 2147483647). This is a lot smaller than 10**20.

     
  4. Jun 17, 2015 #3
    That did it, thanks Mark. Didn't realize that fortran could handle E notation.
     
  5. Jun 17, 2015 #4

    Mark44

    Staff: Mentor

    This should also work: r = 2.5 * 10.0**20, but that's the same as what I wrote using the E notation.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook




Similar Discussions: [Fortran] Integer overflow for a real number?
Loading...