长双精度返回和ctypes [英] long double returns and ctypes

查看:77
本文介绍了长双精度返回和ctypes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个c函数,该函数返回一个 long double 。我想使用ctypes从python调用此函数,并且大多数情况下都有效。设置 so.func.restype = c_longdouble 可以达到目的-除了python的float类型是 c_double 外,如果返回值大于一个double,但在长double的范围内,python仍将inf作为返回值。我使用的是64位处理器,并且 sizeof(long double)是16。

i have a c function which returns a long double. i'd like to call this function from python using ctypes, and it mostly works. setting so.func.restype = c_longdouble does the trick -- except that python's float type is a c_double so if the returned value is larger than a double, but well within the bounds of a long double, python still gets inf as the return value. i'm on a 64 bit processor and sizeof(long double) is 16.

关于绕行的任何想法

推荐答案

我不确定您能做到这一点(例如,使用十进制类或numpy)而不修改c代码吗?无需修改C代码。 ctypes似乎对 long double s的支持确实很差-您根本无法像数字一样操纵它们,您所能做的就是在本机 float Python类型。

I'm not sure you can do it without modifying the C code. ctypes seems to have really bad support for long doubles - you can't manipulate them like numbers at all, all you can do is convert them back and forth between the native float Python type.

您甚至不能使用字节数组作为返回值,而不能使用 c_longdouble ,由于ABI的缘故,浮点值不会像普通返回一样在%eax 寄存器或堆栈中返回值,它们通过硬件专用的浮点寄存器传递。

You can't even use a byte array as the return value instead of a c_longdouble, because of the ABI - floating-point values aren't returned in the %eax register or on the stack like normal return values, they're passed through the hardware-specific floating-point registers.

这篇关于长双精度返回和ctypes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆