Long double in Python

C

Charles Vejnar

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 ?

Thank you.

Charles
 
M

mensanator

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,214
Latest member
JFrancisDavis

Latest Threads

Top