Alvaro Fernandez 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.
Module means
! contains generic functions to average two real numbers of
! any KIND and return that value as the result.
interface means
module procedure :: mean_rr, mean_rd, mean_dr, mean_dd
end interface mean
contains
function mean_rr(r1, r2)
! both args real, result is real
real :: r1, r2, mean_rr
mean_rr = (r1+r2)/2
end function mean_rr
function mean_rd(r1, r2)
! first arg real, other double, result is double
real :: r1
real(kind(0.0d0)) :: r2, mean_rd
mean_rr = (r1+r2)/2
end function mean_rd
function mean_dr(r1, r2)
! second arg real, other double, result is double
real :: r2
real(kind(0.0d0)) :: r1, mean_dr
mean_rr = (r1+r2)/2
end function mean_dr
function mean_dd(r1, r2)
! both args double, result is double
real(kind(0.0d0)) :: r1, r2, mean_dd
mean_rr = (r1+r2)/2
end function mean_dd
end module means
Program test_mean
use means
real :: x, y
real(kind(0.0d0)) :: d, e
... reads to get values for x, y, d, and e
write(*,*) mean(x,y), mean(x,e), mean(d,y), mean(d,e)
end program test_mean
The calls in the test_mean program will call each of the specific
procedures in the module in turn. The generic interface in the
module associates the name MEAN with all four of the individual
procedure names. Which actual procedure gets called depends
on the KINDs of the actual arguments in the call.
--
J. Giles
|