Search code examples
fortraninfinity

exponential of real(16) in fortran


The largest number that is not an infinity in the model of the type of real(16) is 10^4932. I have the exp(10000) in Fortran 90 code that is 8.806818225×10^4342 calculated by simple calculator. I use this simple code:

real(16) :: a
...
a = exp(10000)
...

But it returns infinity. What can I do?


Solution

  • First, who knows what is real(16), the kind numbers are not portable and may mean anything.

    But let's say you want to use real of kind 16 whatever it actually is. Then you have to put real of kind 16 into the exp function

      a = exp(10000._16)
    

    In Fortran expressions never care about their surroundings. exp(10000) is evaluated independently and in the same way in all contexts. You put a default integer there and you got the answer in default real. The answer is too big for default real.

    I strongly suggest using some proper way to defind your kinds than just 16.

    use iso_fortran_env
    integer, parameter :: rp = real128
    real(rp) :: a
    
    a = exp(10000._rp)
    

    is one of the possible ways.