Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

long double returns and ctypes

Tags:

python

ctypes

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?

like image 306
Autoplectic Avatar asked Jan 08 '09 06:01

Autoplectic


1 Answers

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.

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.

like image 107
Adam Rosenfield Avatar answered Oct 06 '22 05:10

Adam Rosenfield