Alvaro Fernandez <[log in to unmask]> wrote:
> I do avoid selected_real_kind(), etc. for just the reason Van Snyder
> mentions - that is the ambiguity I referred to.
>
> I'm afraid I don't follow how the "gluing together" works, though. I
> understand one can have two versions of the algorithm in the body, and
> branch based on the (integer) kind value, but how do you work it so your
> generic has only one set of dummy arguments?
>
> If the dummy arguments in the library routine are for a given kind (say sp =
> kind(0.0)), and the values you attempt to pass in are of a different kind
> (say dp = kind(1.0d0), the compiler will bark. This issue stymied me when I
> tried it, long ago.
I "write" one version of the procedure with default real arguments, and
another with double precision arguments. I give them names like SFOO
and DFOO. Then I write a generic named FOO, referencing the two specifics
SFOO and DFOO. Assume this is all in a module. Then I frequently have
module FOO_M
implicit NONE
private
public FOO, SFOO, DFOO
interface FOO
module procedure SFOO, DFOO
end interface FOO
contains
subroutine SFOO ( A, B, .... )
integer, parameter :: RK = kind(0.0e0)
include "foo.f9h"
end subroutine SFOO
subroutine DFOO ( A, B, .... )
integer, parameter :: RK = kind(0.0d0)
include "foo.f9h"
end subroutine SFOO
end module FOO_M
The real arguments A, B, .... are declared using kind RK ("real kind") in
the include file "foo.f9h":
! subroutine ?FOO ( A, B, .... )
real(rk), intent(...) :: A, ...
real(rk), intent(...) :: B, ...
! Body of FOO
! end subroutine ?FOO
If FOO is a special function (for example), for which I have different
approximations depending on the desired precision, I have branches for
every precision for which I have approximations. I do not make any
assumptions about the precision of default real or double precision.
Since expressions such as epsilon(1.0_rk) are compile-time evaluable,
there is a good chance that the choice of approximation is made at compile
time, not at run time.
--
Van Snyder | What fraction of Americans believe
[log in to unmask] | Wrestling is real and NASA is fake?
Any alleged opinions are my own and have not been approved or disapproved
by JPL, CalTech, NASA, Sean O'Keefe, George Bush, the Pope, or anybody else.
|