[Fortran] Integer overflow for a real number?

In summary, the conversation revolves around defining a real number and encountering an integer overflow warning due to the use of an incorrect notation. The issue is resolved by using E notation for the real number.
  • #1
avikarto
56
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
  • #2
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 avikarto
  • #3
That did it, thanks Mark. Didn't realize that fortran could handle E notation.
 
  • #4
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.
 

1. What is an integer overflow for a real number?

An integer overflow for a real number occurs when a numerical value exceeds the maximum value that can be represented by the data type. In Fortran, this typically happens when a real number is converted to an integer and the value is too large to be stored.

2. How does Fortran handle integer overflow for real numbers?

In Fortran, integer overflow for real numbers is typically handled by either truncating the value to fit within the maximum range of the data type or by returning an error message. The specific behavior may vary depending on the compiler and settings used.

3. What happens if an integer overflow occurs during a calculation with real numbers in Fortran?

If an integer overflow occurs during a calculation with real numbers in Fortran, the behavior will depend on the specific operation being performed. Some operations may return an error message, while others may truncate the result or produce unexpected results.

4. How can I prevent integer overflow for real numbers in Fortran?

To prevent integer overflow for real numbers in Fortran, it is important to ensure that the data types used are appropriate for the values being stored and calculated. Additionally, using appropriate error handling techniques can help identify and address any potential integer overflow issues.

5. Are there any alternative data types in Fortran that can handle larger real numbers without integer overflow?

Yes, Fortran offers several alternative data types such as double precision and quad precision that can handle larger real numbers without integer overflow. However, these data types may have a trade-off in terms of memory usage and performance, so it is important to choose the appropriate data type for the specific application.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
5
Views
1K
  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
9
Views
4K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
11
Views
14K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
1K
  • Programming and Computer Science
Replies
5
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top