Long double in Python

Alex Martelli aleax at mac.com
Mon May 28 20:25:46 EDT 2007


Charles Vejnar <charles.vejnar at isb-sib.ch> wrote:

> Hi,
> 
> Thanks for both suggestions.
> 
> I have indeed tried gmpy. For me, it's not very important to choose between
> numpy or gmpy.
> 
> I hope I won't be off topic. But, as I told you before, I have a C library
> using "long double" numbers and I would like to be able to use it in Python.
> I try to build a module with Swig but I didn't find the C function to use in
> the wrapper to have a "numpy.longdouble" (or the equivalent in gmpy). Is it
> something possible ?

I'm not a SWIG expert, but I believe you can tell SWIG exactly what C
function to use to convert a certain C type to Python; gmpy has a C API
for that purpose, and so no doubt does numpy (I'm the author of gmpy,
but don't know much about numpy, so I'm just guessing about it).  You
may have to write a little bit of custom C code for the purpose... and
I'm not even sure you'll be able to do that with SWIG specifically for
"long double", judging from a document at swig.org which I quote later.

I realize that this answer is far from being fully satisfactory:-(.

Unfortunately, neither pyrex nor ctypes, which today are probably the
two best ways to interface arbitrary C code with Python, have good
support for long double, and, as <http://www.swig.org/Doc1.3/SWIG.html>
says, "The rarely used datatype of long double is not supported by
SWIG":-(.

Writing a custom C extension to wrap long double for Python would not be
hard -- essentially, copy Objects/floatobject.c from the Python source
distribution and edit it just a wee little bit -- but wouldn't
necessarily be sufficient to let you use your desired C library, since
you'd still have to somehow tell SWIG (or whatever other C/Python
interfacing approach) to use it...


Alex
  



More information about the Python-list mailing list