我正在玩SWIG为特定的C库制作python'模块。我遇到了double和float变量的问题。这是一个例子:
/***** simple.c *****/
#include <stdio.h>
double doublefun(double b){
printf("c(%g)",b);
return b+12.5;
}
float floatfun(float b){
printf("c(%f)",b);
return b+12.5;
}
int intfun(int b){
printf("c(%d)",b);
return b+12;
}
/***** simple.i *****/
%module simple
%{
%}
extern double doublefun(double);
extern float floatfun(float);
extern int intfun(int);
和
#***** simpletest.py *****#
import simple
print "i:",simple.intfun(2)
print "f:",simple.floatfun(2.3)
print "d:",simple.doublefun(2.3)
结果是:
i:c(2) 14
f:c(36893488147419103232.000000) 30.0
d:c(2.3) 6.0
任何想法为什么会发生?
P.S。如果我从C代码调用函数....
int main(int argc, char** argv){
printf("i::%d\n",intfun(2));
printf("f::%f\n",floatfun(2.3));
printf("d::%g\n",doublefun(2.3));
return 0;
}
一切都很好:
c(2)i::14
c(2.300000)f::14.800000
c(2.3)d::14.8
答案 0 :(得分:2)
如果您按the first example in the docs:
中所示添加标题%{
#include "simple.h"
%}
然后你应该得到:
i:c(2) 14
f:c(2.300000) 14.8000001907
d:c(2.3) 14.8
请参阅full example。