ctypes双数组编码错误(1.35689368162e-312 - > 0.047098)

时间:2011-09-26 14:34:25

标签: python arrays double ctypes

我有一个接受双数组作为第一个参数的函数

cb_type = CFUNCTYPE(c_void_p, c_double * 2, c_int, c_int)
def mycb(cube, ndim, nparams):
    print "cube before", [v for v in cube]
    for i in range(ndim):
        cube[i] = cube[i] * 10 * math.pi
    print "cube after", [v for v in cube]

我通过通常的cb_type(mycb)将它交给C。

C称之为:

printf("values before: %f, %f\n", Cube[0], Cube[1]);
cb(Cube, *ndim, *npars);
printf("values after : %f, %f\n", Cube[0], Cube[1]);

当我运行程序时,有一些打包/解包或解码问题:

values before: 0.047098, 0.010474           | C
cube before [4.3191267336e-314, 1e-323]     | Python, should be the same values
cube after [1.35689368162e-312, 3.1e-322]   | Python
values after : 0.047098, 0.010474           | C

如何解决编码问题(在python函数中接收[0.047098,0.010474])?

1 个答案:

答案 0 :(得分:0)

我使用 POINTER(c_double)代替 c_double * 2 修复了它。

新输出如下:

values before: 0.905349, 0.270400
cube before [0.905348643442958, 0.2703996141057701]
cube after [28.44236647177882, 8.494854412082024]
values after : 28.442366, 8.494854