Re: How to make a function work with different type and kind type argument
  Home FAQ Contact Sign in
comp.lang.fortran only
 
Advanced search
POPULAR GROUPS

more...

 Up
Re: How to make a function work with different type and kind type argument         

Group: comp.lang.fortran · Group Profile
Author: James Van Buskirk
Date: Sep 4, 2008 10:33

"deltaquattro" gmail.com> wrote in message
news:24061886-05c2-4fe7-b3a1-67fae3ac6858@a18g2000pra.googlegroups.com...
> long time no hear :) sure, I thought of that, but that's not nearly as
> generic as the original sin function. While sin(x) works with any of
> them on any machine, "your" mysin(x) only works with the two KTPs
> Single and Double. Clearly, overloading cannot obtain the same level
> of generality, since even the number of available KTPs varies from
> machine to machine. So, I was wondering how "complete" generality is
> achieved in the intrisic functions, and if this can be replicated by
> user-defined functions.

For the SIN function it's rather awkward to write a generic version
because even after argument reduction the result is obtained by
evaluating a minmax polynomial (actually for SIN perhaps a minmax
polynomial for sin(x)/x) which has different orders for different KINDs.
This means that you would almost have to develop and test different
code for each KIND.

For procedures where the same code works for different KINDs, you can
write a template procedure where the KIND is called something like
'wp' (for Working Precision) and INCLUDE this procedure in various
MODULEs, and USEing those MODULEs in others, perhaps with rename
clauses if you want specific names to be available for the different
versions. I have an example of this in

http://home.comcast.net/~kmbtib/Fortran_stuff/GENERIC_BLAS.ZIP .

This style of template programming will work across user-defined
types or across INTEGER KINDs or across LOGICAL KINDs or across
REAL KINDs, but not on a combination that mixes the above sets.
For this kind of genericity (as for order statistics just to give
one example) we can still achieve template programming but it's
necessary to use implicit typing. The template type now takes
the place of the 'wp' KIND and this means we are limited to no
more than 26 independent template types of this ilk. I have posted
an example at

http://home.comcast.net/~kmbtib/Fortran_stuff/funr1.ZIP .

This issue about not knowing in advance how many KIND types will
be available is insoluble, AFAIK. What I do in my GENERIC_BLAS
example is to write out different versions for the different
numbers of KIND types.

--
write(*,*) transfer((/17.392111325966148d0,6.5794487871554595D-85, &
6.0134700243160014d-154/),(/'x'/)); end
no comments
diggit! del.icio.us! reddit!