2 questions in one thread : Fortran 90

In summary, the program mod2 does the Euclidean division between two numbers, which is very inefficient. However, it is still fast after all. However, it seems that the number that is too big for Fortran 90 to compute is 10 billion. There may be a limit to how big a number Fortran can compute. Also, if you want to calculate pi with 1000000 digits, you might need to use a program that is written in C, rather than Fortran.
  • #1
fluidistic
Gold Member
3,923
261
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
  • #2
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.
 
  • #3
Thank you very much alphysicist. Your answer is very informative. I'll take some time to check it out in details.
 

1. What is Fortran 90?

Fortran 90 is a computer programming language used for scientific and engineering applications. It is an updated version of the original Fortran language, with added features and improvements in syntax, data types, and control structures.

2. What are the main differences between Fortran 90 and Fortran 77?

Fortran 90 introduced several new features such as modules, dynamic memory allocation, recursion, and improved array handling capabilities. It also has a more concise and modern syntax compared to Fortran 77.

3. How do I declare variables in Fortran 90?

In Fortran 90, variables can be declared using the INTEGER, REAL, DOUBLE PRECISION, or CHARACTER keywords, followed by the variable name. Optional attributes such as DIMENSION, PARAMETER, and INTENT can also be included.

4. Can Fortran 90 be used for object-oriented programming?

Fortran 90 does not have built-in support for object-oriented programming, but it does have some features that can be used to achieve similar functionality, such as MODULES and TYPE declarations.

5. Is Fortran 90 still relevant and widely used today?

Yes, Fortran 90 is still used in many scientific and engineering fields, particularly in high-performance computing applications. However, it has been largely replaced by newer versions of Fortran, such as Fortran 95 and Fortran 2003, which offer even more advanced features and improvements.

Similar threads

  • Programming and Computer Science
Replies
4
Views
1K
  • Programming and Computer Science
Replies
4
Views
588
  • Programming and Computer Science
Replies
12
Views
953
  • Programming and Computer Science
Replies
8
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
5
Views
4K
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
19
Views
5K
  • Programming and Computer Science
Replies
2
Views
1K
Back
Top