Long double in Python
mensanator at aol.com
mensanator at aol.com
Fri May 25 23:22:38 EDT 2007
On May 25, 2:40?pm, Charles Vejnar <charles.vej... at isb-sib.ch> 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" ?
> 2nd solution : Numpy. In memory, a "numpy.longdouble" is a C "long double" or
> a string ?
Have you tried gmpy?
import gmpy
print 'default precision'
for e in xrange(10):
r = gmpy.mpq(2**e,3**e) # rational
print gmpy.mpf(r) # float
print
print '128-bit precision'
for e in xrange(10):
r = gmpy.mpq(2**e,3**e)
print gmpy.mpf(r,128)
## default precision
## 1.0
## 0.666666666666666666667
## 0.444444444444444444444
## 0.296296296296296296296
## 0.197530864197530864198
## 0.131687242798353909465
## 0.08779149519890260631
## 0.0585276634659350708733
## 0.0390184423106233805822
## 0.0260122948737489203882
##
## 128-bit precision
## 1.0
## 0.6666666666666666666666666666666666666667
## 0.4444444444444444444444444444444444444444
## 0.2962962962962962962962962962962962962963
## 0.1975308641975308641975308641975308641975
## 0.1316872427983539094650205761316872427984
## 0.08779149519890260631001371742112482853224
## 0.05852766346593507087334247828074988568816
## 0.03901844231062338058222831885383325712544
## 0.02601229487374892038815221256922217141696
>
> Thank you.
>
> Charles
More information about the Python-list
mailing list