Fortran 2 questions in one thread : Fortran 90

  • Thread starter Thread starter fluidistic
  • Start date Start date
  • Tags Tags
    Fortran Thread
Click For Summary
SUMMARY

This discussion centers on the limitations of Fortran 90 regarding integer overflow and the handling of large numbers. The user encountered a runtime error when attempting to compute Euclidean division with numbers exceeding 10 billion, revealing that Fortran's default integer size can lead to overflow issues. The solution involves using the g95 compiler's INTEGER(KIND=8) to handle larger integers, allowing for values up to approximately 9 x 1018. Additionally, the conversation touches on arbitrary-precision arithmetic, explaining how languages like C can manage calculations of pi with extensive precision compared to Fortran's default capabilities.

PREREQUISITES
  • Understanding of Fortran 90 syntax and structure
  • Familiarity with the g95 compiler and its INTEGER(KIND) specifications
  • Knowledge of integer overflow concepts in programming
  • Basic comprehension of arbitrary-precision arithmetic
NEXT STEPS
  • Research how to implement INTEGER(KIND=8) in Fortran 90 programs
  • Explore the g95 compiler documentation for integer kind specifications
  • Learn about arbitrary-precision arithmetic libraries available for Fortran
  • Investigate how other programming languages handle large number computations, particularly C and its libraries
USEFUL FOR

This discussion is beneficial for Fortran developers, computer scientists dealing with numerical computations, and anyone interested in understanding integer limitations and precision arithmetic in programming languages.

fluidistic
Gold Member
Messages
3,931
Reaction score
281
Hi,
I'm laughing, after having passed more than one hour on a very simple program I could finish it. It does the Euclidean division between 2 numbers that one type.

It is very inefficient I agree. I gave up doing it in a more efficient way in order to simplify it (for my brain at least). Nevertheless up till a billion it gives the answer almost instantly and take less than 5 seconds for a number like a billion, so it's fast after all.

program mod2
implicit none

Integer :: a,q,r,i,k

write(*,*)'Enter a number and its divisor'
read(*,*)a,k
q=k
i=1
do while (q<a)
q=k*i
i=i+1
end do
if (q>a) then
q=q-k
endif
i=i-2
r=a-i*k
if (r==k) then
write(*,*)a,'=',i+1,'*',k,'+0'
Else
write(*,*)a,'=',i,'*',k,'+',r
endif
end program mod2
Ok here are my questions. I've tried a number greater than 10 billions and I got this message : "At line 9 of file mod2.f90 (unit = 5,file = 'stdin') Fortran runtime error: Integer overflow while reading item 1"
So my guess is that the number is too big. (Strangely it says "runtime error" when in fact it didn't even tried to compute it and the message appears instantly). Has Fortran 90 a limit when computing numbers? If so, is the limit at 10 billions? It seems to me a very small limit for computer calculus.

Second question : How can programs like Mathematica, Maple, etc. give the number pi with say 1000000 digits if (my guess is that they are written in C) fortran and other programming languages cannot work with more than 8 digits or so?

Thanks in advance.
 
Technology news on Phys.org
Hi fluidistic,

fluidistic said:
Hi,
I'm laughing, after having passed more than one hour on a very simple program I could finish it. It does the Euclidean division between 2 numbers that one type.

It is very inefficient I agree. I gave up doing it in a more efficient way in order to simplify it (for my brain at least). Nevertheless up till a billion it gives the answer almost instantly and take less than 5 seconds for a number like a billion, so it's fast after all.


Ok here are my questions. I've tried a number greater than 10 billions and I got this message : "At line 9 of file mod2.f90 (unit = 5,file = 'stdin') Fortran runtime error: Integer overflow while reading item 1"
So my guess is that the number is too big. (Strangely it says "runtime error" when in fact it didn't even tried to compute it and the message appears instantly). Has Fortran 90 a limit when computing numbers? If so, is the limit at 10 billions? It seems to me a very small limit for computer calculus.

In fortran you essentially have to specify the range for your integer values. If you don't specify it (using KIND), there is a default. So for example for the g95 compiler, the default is for integer to have 4 bytes, and so the maximum integer is about 2 x 109 (and the minimum is about -2 x 109).

If I change the third line in your program to

Code:
Integer(kind=8) :: a,q,r,i,k

and compile it with g95, then it is an 8 byte integer, and the maximum integer allowed is about 9 x 1018. (Using g95 you also can specify kind=1 and kind=2; the 1 byte integer would only allow integers in the range -128 to 127.)



If you want to see the largest integer that can fit in the variable a (for example), then you can put this line in your program:

Code:
print*,  "Largest value for a is ", huge(a)

When I use this with the KIND=8, it gives that the largest acceptable value for a is 9223372036854775807 (hope I didn't miss a digit).




Also, the kind values that the g95 compiler uses are KIND=1,2,4, and 8 for integers. But I believe the actual numbers are up to the people writing the compilers, and I'm fairly certain that I've used compilers that instead use KIND=1,2,3 and 4 for example. You might need to read the documentation for your compiler, or you could just play around with it a while until you find the acceptable KIND numbers, and then use the HUGE function to find the limits on the integers.




Second question : How can programs like Mathematica, Maple, etc. give the number pi with say 1000000 digits if (my guess is that they are written in C) fortran and other programming languages cannot work with more than 8 digits or so?

Thanks in advance.

You might be interested in this article from wikipedia:

http://en.wikipedia.org/wiki/Arbitrary-precision_arithmetic

I've only skimmed through it, but I think this sentence is most relevant to your question (it is the first sentence from the "Applications" section):

Arbitrary-precision arithmetic is considerably slower than arithmetic using numbers that fit entirely within processor registers, since the latter are usually implemented in hardware arithmetic whereas the former must be implemented in software in most cases


(Calculations of pi are mentioned in that section also.)


It also has a link to an arbitrary-precision package for fortran. I've heard of it before, but have never looked into it at all.
 
Thank you very much alphysicist. Your answer is very informative. I'll take some time to check it out in details.
 
We have many threads on AI, which are mostly AI/LLM, e.g,. ChatGPT, Claude, etc. It is important to draw a distinction between AI/LLM and AI/ML/DL, where ML - Machine Learning and DL = Deep Learning. AI is a broad technology; the AI/ML/DL is being developed to handle large data sets, and even seemingly disparate datasets to rapidly evaluated the data and determine the quantitative relationships in order to understand what those relationships (about the variaboles) mean. At the Harvard &...

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K
  • · Replies 8 ·
Replies
8
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 37 ·
2
Replies
37
Views
5K
  • · Replies 19 ·
Replies
19
Views
7K