开发者

ctypes double array encodes wrong (1.35689368162e-312-->0.047098)

I have a function that accepts a double array as first parameter

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]
    fo开发者_运维知识库r i in range(ndim):
        cube[i] = cube[i] * 10 * math.pi
    print "cube after", [v for v in cube]

I hand it over to C via the usual cb_type(mycb).

C calls it:

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

When I run the program, there is some packing/unpacking or decoding issue:

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

How can I fix the encoding issue (receive [0.047098, 0.010474] in the python function)?


I fixed it by using POINTER(c_double) instead of c_double * 2.

The new output looks like this:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜