我这样通过 cgo 调用 C 函数:
var _outptr_7 C.double
var _outptr_8 C.double
var kk uintptr = uintptr(unsafe.Pointer(&_outptr_7))
gogsl.InitializeGslFunction(f)
_result := int32(C.gsl_integration_qags((*C.gsl_function)(unsafe.Pointer(f.CPtr())), C.double(a), C.double(b), C.double(epsabs), C.double(epsrel), C.size_t(limit), (*C.gsl_integration_workspace)(unsafe.Pointer(workspace.Ptr())), (*C.double)(&_outptr_7), (*C.double)(&_outptr_8)))
fmt.Printf("%10.10X\n",kk)
return _result, *(*float64)(unsafe.Pointer(&_outptr_7)), *(*float64)(unsafe.Pointer(&_outptr_8))
现在,kk这里用于调试目的。我修改了 C 函数gsl_integration_qags以输出它收到的第 8 个参数(即&_outptr_7,_<n>从 0 开始的计数!)
(InitializeGslFunction出于此处的目的可以忽略...)
这两个值 -kk以及 C 函数输出 - 完全匹配。
但是,我得到的数字与 C 函数返回的数字不同- 它通过间接方式设置第 8 个参数。我确定这是正确的,我看过它在 gdb 中工作。
它看起来像这样:(省略了(gdb)我点击回车重复命令的地方)
Breakpoint 3, qags (f=0xc20800a260, a=0, b=1, epsabs=0, epsrel=9.9999999999999995e-08, limit=1000, workspace=0x782ec0, result=0xc208031e28, abserr=0xc208031e20, q=0x7ffff79ad12c <gsl_integration_qk21>)
at qags.c:479
479 *result = res_ext;
(gdb) n
480 *abserr = err_ext;
(gdb) p *result
$1 = -4.0000000000000853
(gdb) p result
$2 = (double *) 0xc208031e28
(gdb) n
482 if (err_ext == GSL_DBL_MAX)
485 if (error_type || error_type2)
513 double max_area = GSL_MAX_DBL (fabs (res_ext), fabs (area));
515 if (!positive_integrand && max_area < 0.01 * resabs0)
520 double ratio = res_ext / area;
522 if (ratio < 0.01 || ratio > 100.0 || errsum > fabs (area))
526 goto return_error;
535 if (error_type > 2)
540 if (error_type == 0)
542 return GSL_SUCCESS;
573 }
gsl_integration_qags (f=0xc20800a260, a=0, b=1, epsabs=0, epsrel=9.9999999999999995e-08, limit=1000, workspace=0x782ec0, result=0xc208031e28, abserr=0xc208031e20) at qags.c:53
53 return status ;
54 }
相关分类