我想实现一个子程序,它可以使用单精度,双精度和扩展精度的实数.我能想出的唯一解决方案如下面的代码所示.这个解决方案有效但我必须复制代码3次.可以避免此代码重复吗?
module mymodule
....
! some code here
interface my_func
module procedure my_func_sp
module procedure my_func dp
module procedure my_func_ep
end interface
contains
subroutine my_func_sp(x,y)
real(kind=sp), dimension(:) :: x,y
... LONG IMPLEMENTATION HERE ...
end subroutine
subroutine my_func_dp(x,y)
real(kind=dp), dimension(:) :: x,y
... LONG IMPLEMENTATION HERE THAT IS EXACTLY THE SAME AS ABOVE ...
end subroutine
subroutine my_func_ep(x,y)
real(kind=ep), dimension(:) :: x,y
... LONG IMPLEMENTATION HERE THAT IS EXACTLY THE SAME AS THE TWO ABOVE ...
end subroutine
end module
Run Code Online (Sandbox Code Playgroud)
可以避免此代码重复吗? 不是,这是Fortran的工作方式.你可以:
include其写入每个子例程中的该文件中.请注意包含的语句对各种类型都有效.还要注意相同的陈述可以跨越各种类型.如果,例如,您的包含线包括一个宽容的比较,因为很多数字代码做,你可能要采取的公差调整特殊照顾WRT的那种.