[Fortran] Integer overflow for a real number?

Click For Summary

Discussion Overview

The discussion revolves around an issue with defining a real number in Fortran, specifically related to integer overflow warnings when using exponential notation. Participants explore the implications of using different kinds and notations for real numbers in the context of the g95 compiler.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes an attempt to define a real number using the expression r=2.5*10**20 and receives an integer overflow warning, despite not declaring any integer variables.
  • Another participant suggests that the compiler may be interpreting 10**20 as an integer calculation, leading to overflow, and recommends using scientific notation (r = 2.5E20) instead.
  • Some participants note that using real(kind=10) should theoretically accommodate the value, but it does not resolve the warning in their experience.
  • There is mention of the selected_real_kind(20) function returning a value of 16, which some participants interpret as indicating the appropriate kind for their needs.
  • One participant expresses confusion about why smaller values (e.g., 10**(-30)) do not trigger similar warnings.
  • Several participants acknowledge the effectiveness of using E notation for defining real numbers in Fortran.

Areas of Agreement / Disagreement

Participants generally agree that using E notation resolves the integer overflow issue, but there is no consensus on the underlying cause of the warning or the best approach to avoid it.

Contextual Notes

Participants express uncertainty regarding the behavior of the compiler in interpreting exponential expressions, and there are unresolved questions about the implications of different kinds and notations.

avikarto
Messages
56
Reaction score
9
I am trying to define a real number as follows:
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:
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
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!
 
Technology news on Phys.org
avikarto said:
I am trying to define a real number as follows:
Fortran:
real*8 r
r=2.5*10**20
This should be written as
Code:
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.

avikarto said:
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:
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
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!
 
  • Like
Likes   Reactions: avikarto
That did it, thanks Mark. Didn't realize that fortran could handle E notation.
 
avikarto said:
That did it, thanks Mark. Didn't realize that fortran could handle E notation.
This should also work: r = 2.5 * 10.0**20, but that's the same as what I wrote using the E notation.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 3 ·
Replies
3
Views
2K
  • · Replies 9 ·
Replies
9
Views
5K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
11
Views
16K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 2 ·
Replies
2
Views
2K