Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

2 questions in one thread : Fortran 90

  1. Nov 11, 2008 #1


    User Avatar
    Gold Member

    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.

    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.
  2. jcsd
  3. Nov 12, 2008 #2


    User Avatar
    Homework Helper

    Hi fluidistic,

    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 (Text):
    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 (Text):
    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.

    You might be interested in this article from wikipedia:


    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.
  4. Nov 12, 2008 #3


    User Avatar
    Gold Member

    Thank you very much alphysicist. Your answer is very informative. I'll take some time to check it out in details.
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook