Hi,
What is the modern recommended way of working with double precision numbers?
In the the appendix B.6 in "Modern Fortran Explained" (Metcalf, Reid &
Cohen) it says:
The double precision real data type has been replaced by the real type
of kind(0.d0).
Since the B appendix is about "Deprecated features", this suggests
that the "d" -- as in 0.d0 --- is deprecated and can be made
obsolescent in a future revision of the Fortran standard. Is my
understanding correct?
Given this information, here is what I do:
real(dp) :: x
x = 1.5_dp
and so on. Then at one point in the program, I define:
integer, parameter :: dp=kind(0.d0)
However, it has recently been pointed out to me that even the
kind(0.d0) declaration is outdated, because of the use of 0.d0. As
such, one alternative is:
integer, parameter :: dp = selected_real_kind(p=precision(0.0)+1)
which on all computers that I tried gives double precision. However,
one problem with this approach is that if on some future architecture,
there was a precision between single (p=6) and double (p=15), for
example with p=10, then it wold get selected instead, as we are asking
for p=precision(0.0)+1=7. However, CPUs can handle different
precisions with
quite different efficiencies. If one were to choose a precision that
was somewhere between single and double (if available), that could
yield substantially less efficient arithmetic than standard "double
precision" which will, almost certainly, be implemented most
efficiently.
So another way is to ask directly for double precision as:
integer, parameter :: dp = selected_real_kind(p=15)
And maybe this should be the recommended way?
I would be very interested in your opinions, so that I can update the
http://fortran90.org/ pages with the right way to do this. If there is
no single right way, then I want to put there the recommended ways.
Regards,
Ondrej Certik
|