This is an example about the high precision in Fortran, at first we see a normal Program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 |
write your number: 4 your result: 1.33333337 |
Now we use selected_real_kind(32) to get an higher precision data type:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
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:
1 2 3 |
write your number: 4 your result: 1.33333333333333333333333333333333327 |
looks much better than the first.