Fortran high precision example

This is an example about the high precision in Fortran, at first we see a normal Program:

program hello
    implicit none
    real   :: div
    real   :: i
    Print  *,"write your number:"
    read   *, i
    i= div(i)
    print  *,"your result:",i
end program Hello

real function div( a )
  implicit none
  real  :: a
  div   = a/3
end function div

our Result looks like this:

 write your number:   
4      
 your result:   1.33333337

 

Now we use selected_real_kind(32) to get an higher precision data type:

program hello
    implicit none
    real(kind=selected_real_kind(32)):: div
    real(kind=selected_real_kind(32)) :: i
    Print  *,"write your number:"
    read   *, i
    i= div(i)
    print  *,"your result:",i
end program Hello

real(kind=selected_real_kind(32)) function div( a )
  implicit none
  real(kind=selected_real_kind(32))  :: a
  div   = a/3
end function div

Now we get this result:

 write your number:   
4   
 your result:   1.33333333333333333333333333333333327

looks much better than the first.

4 thoughts on “Fortran high precision example”

  1. Fantastic beat ! I would like to apprentice even as you amend
    your site, how can i subscribe for a blog website?
    The account aided me a acceptable deal. I have
    been a little bit familiar of this your broadcast offered shiny transparent idea

  2. Hello There. I discovered your blog using msn. This is a very well
    written article. I’ll make sure to bookmark it and return to learn more of your helpful info.
    Thank you for the post. I will certainly return.

  3. Good post. I learn something totally new and challenging on blogs I stumbleupon everyday.
    It will always be interesting to read through content from other writers and practice something
    from other web sites.

  4. I needed to thank you for this very good read!!
    I definitely loved every bit of it. I have got you book-marked to
    check out new things you post…

Leave a Reply

Your email address will not be published.

12 − nine =