What is the correct way to assign a bound of magnitude 10**270 in Fortran?

  • Context: Fortran 
  • Thread starter Thread starter autobot.d
  • Start date Start date
  • Tags Tags
    Fortran
Click For Summary
SUMMARY

The correct way to assign a bound of magnitude 10**270 in Fortran is to use double precision notation. Attempting to assign DOUBLE PRECISION :: bound = 10**270 results in an arithmetic overflow because the expression is evaluated as an integer. To avoid this, use 10.0d0 ** 270 or 10.0d270, ensuring the "d" exponent is used to indicate double precision. Using 10.0 ** 270 will also cause an overflow due to single precision limitations.

PREREQUISITES
  • Understanding of Fortran data types, specifically double precision
  • Familiarity with exponentiation in Fortran
  • Knowledge of numeric limits in single and double precision
  • Basic experience with Fortran syntax and error handling
NEXT STEPS
  • Research Fortran data type specifications and their limits
  • Learn about numeric precision and overflow handling in Fortran
  • Explore advanced Fortran features for numerical computations
  • Study best practices for using constants in Fortran
USEFUL FOR

This discussion is beneficial for Fortran developers, numerical analysts, and anyone working with high-precision calculations in scientific computing.

autobot.d
Messages
67
Reaction score
0
I want to find a bound for around magnitude 10**270 which is within bounds of double precision but I get

DOUBLE PRECISION :: bound = 10**270

Error: Arithmetic overflow at (1)
.\q2.f03:70.4:


where 1 is beneath the first *.

I am sure it is something simple I am overlooking...
 
Technology news on Phys.org
Since 10 and 270 are both integers, 10**270 is evaluated as an integer expression, and then the result is converted to double precision to assign it to the variable. You got the overflow because 10**270 can't be represetned as an integer value.

You could write 10.0d0 ** 270, or better, just 10.0d270.

Note you must use a "d" exponent (not "e") in a constant to make it double precision. 10.0 ** 270 will still give an overflow, because 10.0 is a single precision constant and the maximum value that can be represented in single precision is about 1038.
 

Similar threads

  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 18 ·
Replies
18
Views
7K
  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 4 ·
Replies
4
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K