473,480 Members | 1,531 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Long double in Python

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
1 8041
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

15
2250
by: Ville Vainio | last post by:
Pythonic Nirvana - towards a true Object Oriented Environment ============================================================= IPython (by Francois Pinard) recently (next release - changes are...
9
1936
by: Ali | last post by:
I did the folloing in python shell: >>> x = 5.07e-25 >>> x = long(x) >>> print x 0L Um... I was under the impresion that long numbers had very very long precision. But, it seems that in...
15
3889
by: michael.mcgarry | last post by:
Hi, I have a question about floating point precision in C. What is the minimum distinguishable difference between 2 floating point numbers? Does this differ for various computers? Is this...
5
9112
by: Daniel Rudy | last post by:
How does one covert a interger number in a unsigned long long int (64-bit) to long double (80-bit) storage? I looked at math.h and I found function that convert double to long long, but didn't...
10
18734
by: Bryan Parkoff | last post by:
The guideline says to use %f in printf() function using the keyword float and double. For example float a = 1.2345; double b = 5.166666667; printf("%.2f\n %f\n", a, b);
0
4278
by: Dean N. Williams | last post by:
Dear Python and Mac Community, I have just successfully built gcc version 4.1.0 for my Mac OS X 10.4.6. gcc -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with:...
67
9809
by: lcw1964 | last post by:
This may be in the category of bush-league rudimentary, but I am quite perplexed on this and diligent Googling has not provided me with a clear straight answer--perhaps I don't know how to ask the...
52
5915
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I...
10
3789
by: ratcharit | last post by:
Currently using cosine function in math.h Currently I get: 1 = cos(1e^-7) Is there another way for cos to return value of high accuracy say: 0.999999 = cos(1e^-7)
0
7044
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
6908
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
6741
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5341
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4782
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4483
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
2995
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2985
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
563
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.