Long double in Python

Robert Kern robert.kern at gmail.com
Fri May 25 18:52:24 EDT 2007


Charles Vejnar wrote:
> Hi,
> 
> I have a C library using "long double" numbers. I would like to be able to 
> keep this precision in Python (even if it's not portable) : for the moment I 
> have to cast the "long double" numbers to "double" numbers.
> 
> 1st solution . Is it possible that by re-compiling Python, Python Float object 
> becomes "long double" C type instead of "double" ?

It might be possible, but I recommend against it.

> 2nd solution : Numpy. In memory, a "numpy.longdouble" is a C "long double" or 
> a string ?

Well, it's always a sequence of bytes, but yes, it is a C "long double,"
whatever that happens to be on your platform. On my Intel MacBook:


In [55]: import numpy

In [56]: x = numpy.longdouble(1e200)

In [57]: x
Out[57]: 9.99999999999999969733e+199

In [58]: x * x
Out[58]: 9.99999999999999939489e+399

In [59]: x * x * x
Out[59]: 9.99999999999999909194e+599

In [60]: 1e200 * 1e200 * 1e200
Out[60]: inf

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
 that is made terrible by our own mad attempt to interpret it as though it had
 an underlying truth."
  -- Umberto Eco




More information about the Python-list mailing list