fortran - Calling function with configurable real precision -
the goal:
have function work configurable working precision.
when try this:
program vierkantsvergelijking implicit none integer, parameter :: dp = kind(0.d0) integer, parameter :: sp = kind(0.0) print *, algoritme1(-5771.,2.,dp) contains function algoritme1(b,c,wp) result( solution) integer :: wp ! working precision real(kind=wp) :: b,c,d real(kind=wp), dimension(2) :: solution d = sqrt((b/2)**2 - c) solution(1) = -b/2 + d solution(2) = -b/2 - d end function algoritme1 end program
i get: error: type mismatch in argument 'b' @ (1); passed real(4) unknown
why not working , how can achieve goal?
yes, or rather no, that's not going work, not no how. intel fortran compiler complains, line:
real(kind=wp) :: b,c,d
that
a kind type parameter must compile-time constant. [wp]
it makes same complaint real(kind=wp), dimension(2) :: solution
too. deep-rooted feature of fortran.
to want have define generic interface, along these lines
interface algoritme1 procedure :: algoritme1_sp, algoritme1_dp end interface
and write code both procedures. compiler can determine 1 called function signature; presumably 1 have sp
arguments, other dp
arguments.
you might think means fortran doesn't generic procedures, i'll leave question sophists , language nit-pickers. worth while search around generic programming in fortran
; here on there tricks , tips on how better avoid writing multiple implementations of programmer believes (at odds compiler) 'same' code.
Comments
Post a Comment