Real function circle ( x ) result ( ans ) real, intent ( in ), value :: x ans = sqrt ( 4. , 3.14 ) name = "cos" cf = choose_func ( c_loc ( name )) f => wapper print *, real_integral ( f, 0. , 2.0 ) name = "sin" cf = choose_func ( c_loc ( name )) ! c 函数指针赋值给 FortranĬall c_f_procpointer ( cf, f ) print *, real_integral ( f, 0. Real ( c_float ), intent ( in ), value :: x end functionĬharacter ( len = :), allocatable, target :: name procedure ( real_func ), pointer :: f type ( c_funptr ) :: cf ! 使用 Fortran 原生的函数指针į => circle print *, real_integral ( f, 0. programmathsimplicitnonereal::r,areareal,parameter::pi4.atan(1. Real ( c_float ) function float_func ( x ) import :: c_float Basic fortran program Let us write a program to print the area of a circle in your stdout (your terminal). Type ( c_ptr ), value, intent ( in ) :: c_name type ( c_funptr ) :: choose_func end function Impure real function real_integral ( f, a, b, N ) result ( ans ) procedure ( real_func ), pointer, intent ( in ) :: f real, intent ( in ) :: a, b integer, optional :: N integer :: Npoints, i real :: x, h Npoints = 10000 if ( present ( N )) Npoints = N h = ( b - a ) / Npoints ans = 0.0 do i = 1, Npoints x = a + ( i - 0.5 ) * h ans = ans + f ( x ) end doĮnd module integral_m program test use iso_c_bindingįunction choose_func ( c_name ) bind ( c ) import :: c_ptr, c_funptr ! const char*,但是 `ios_c_binding` 只有`c_ptr`类型 Real function real_func ( x ) real, intent ( in ), value :: x end function
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |