By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,813 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,813 IT Pros & Developers. It's quick & easy.

Long double in Python

P: n/a
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
May 25 '07 #1
Share this Question
Share on Google+
1 Reply


P: n/a
On May 25, 2:40?pm, Charles Vejnar <charles.vej...@isb-sib.chwrote:
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

May 26 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.