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

Home Posts Topics Members FAQ

Lossless Number Conversion

Is there any library for Python that implements a kind of universal
number object. Something that, if you divide two integers, generates a
ratio instead of a float, or if you take the square root of a negative,
generates a complex number instead of raising an exception? Lisp has
something like this, and it makes number crunching much more convenient.

Chris
Aug 28 '05 #1
3 1367

Chris Spencer wrote:
Is there any library for Python that implements a kind of universal
number object. Something that, if you divide two integers, generates a
ratio instead of a float, or if you take the square root of a negative,
generates a complex number instead of raising an exception? Lisp has
something like this, and it makes number crunching much more convenient.

Chris


The GMPY module has unlimited precision rationals:
from gmpy import * r = mpq(1,1) # create the rational 1/1
for i in range(2,50): r = r + mpq(1,i) # add the rational 1/i to the running sum
print r
3/2
11/6
25/12
137/60
49/20
363/140
761/280
7129/2520
7381/2520
83711/27720
86021/27720
1145993/360360
1171733/360360
1195757/360360
2436559/720720
42142223/12252240
14274301/4084080
275295799/77597520
55835135/15519504
18858053/5173168
19093197/5173168
444316699/118982864
1347822955/356948592
34052522467/8923714800
34395742267/8923714800
312536252003/80313433200
315404588903/80313433200
9227046511387/2329089562800
9304682830147/2329089562800
290774257297357/72201776446800
586061125622639/144403552893600

So you can keep absolute precision all the way to the end
of the calculation. And you can always convert it to a float
by dividing the numerator by the denominator:
print mpf(r.numer())/mpf(r.denom())

4.47920533832942505756047179296

Aug 29 '05 #2
Am Sonntag, den 28.08.2005, 21:36 +0000 schrieb Chris Spencer:
Is there any library for Python that implements a kind of universal
number object. Something that, if you divide two integers, generates a
ratio instead of a float, or if you take the square root of a negative,
generates a complex number instead of raising an exception? Lisp has
something like this, and it makes number crunching much more convenient.


Yes and no. There isn't general solution. But for example you want to
allow complex number, just use cmath.

Andreas

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQBDErdeHJdudm4KnO0RAkF9AKDQNbpEdi+8UwJe3O/CCTWBq2Mx+ACeIFZn
WxWP3ZgRG4vmPt7zemJNLDw=
=6NkG
-----END PGP SIGNATURE-----

Aug 29 '05 #3
Chris Spencer wrote:
Is there any library for Python that implements a kind of universal
number object. Something that, if you divide two integers, generates a
ratio instead of a float, or if you take the square root of a negative,
generates a complex number instead of raising an exception? Lisp has
something like this, and it makes number crunching much more convenient.

Chris


If you are on any Unix-like platform, you might want to check out clnum.
It works like the gmpy package but has complex versions of rationals
and arbitrary precision floats.

http://calcrpnpy.sourceforge.net/clnum.html
Aug 29 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
8251
by: John Bentley | last post by:
John Bentley writes at this level: If we think about our savings accounts then division never comes in (as far as I can see). We deposit and withdraw exact amounts most of the time. Occasionaly...
17
3998
by: jake1138 | last post by:
Here is a function I have to get a number at the end of a string. I'm posting this in case it proves helpful to someone. Comments are welcome. int getnum(char *str) { char buffer; char *buf...
9
16982
by: fooboo | last post by:
Does anyone know if a easier way (built in function, or something) that can verify that a string is an alphanumeric number? Here is what I am doing now: for(i=0; i < strlen(temp); i++){...
5
2287
by: Pavils Jurjans | last post by:
Hello, I am somewhat lost in the implicit/expicit possible/impossible type casting in C#... I need to write a class, which among other things, must have wat to read a numeric value type, and...
1
282
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
0
1701
by: Daniel Nogradi | last post by:
Hi all, Last time I checked PIL was not able to apply lossless transformations to jpeg images so I've created Python bindings (or is it a wrapper? I never knew the difference :)) for the...
1
5686
by: PAF | last post by:
Hi, I'm trying to evaluate a sum expression trougth .NET 2 System.Xml.XPath.XPathNavigator object. My code is : Dim document As New System.Xml.XmlDocument() document.LoadXml("<?xml...
2
2187
by: FAQ server | last post by:
----------------------------------------------------------------------- FAQ Topic - Why does 1+1 equal 11? or How do I convert a string to a number?...
23
9744
by: neha_chhatre | last post by:
which is the best format specifier(data type) if i have to work with decimal number. also please tell me the syntax for truncating a decimal number please reply as soon as possible
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
7045
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7087
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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
1300
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
182
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.