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.
any ideas on getting around this (e.g. using the decimal class or numpy) without modifying the c code?
I'm not sure you can do it without modifying the C code. ctypes seems to have really bad support for long double
s - 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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With