473,387 Members | 1,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

working with VERY large 'float' and 'complex' types

Greetings Python'ers:

I'm just an amature who occasionally uses Python for complex mathematical
models. The current model I'm working with occasionally generates really
large numbers that are either "float" or "complex" types. These numbers are
so large that I either get an overflow error, or some funky code like #INF
or 1.#INDj. However I really need these numbers to be calculated (although
precision isn't key). Is there a way to get python to increase the size
limit of float and complex numbers? I should mention that I'm using a lot of
pre-made modules and functions like math.exp() and scipy.special.erf() that
don't seem to be able to use available types like "Decimal" or "FixedPoint"
(especially since these don't seem to handle complex numbers).

Since I learn best by example, how could one solve the following problem:

from math import exp
x=1000.
z=exp(x)


so that z returns an actual value

Thanks in advance for any advice!

Todd
Sep 14 '05 #1
5 5361
"Todd Steury" <sc******@iwon.com> writes:
or 1.#INDj. However I really need these numbers to be calculated (although
precision isn't key). Is there a way to get python to increase the size
limit of float and complex numbers?
Python just uses machine doubles by default. You might be able to edit
the source so it uses, say, 80-bit extended floats if you're on a machine
that supports them (like the x86). Those do support larger exponents.

You could also use something like gmpy, which supports arbitrary size
and arbitrary precision numbers. Of course it's slow by comparison.
Since I learn best by example, how could one solve the following problem:

from math import exp
x=1000.
z=exp(x)


so that z returns an actual value


You could rearrange your formulas to not need such big numbers:

x = 1000.
log10_z = x / math.log(10)
c,m = divmod(log10_z, 1.)
print 'z = %.5fE%d' % (10.**c, m)
Sep 14 '05 #2
Todd Steury wrote:
Greetings Python'ers:

I'm just an amature who occasionally uses Python for complex mathematical
models. The current model I'm working with occasionally generates really
large numbers that are either "float" or "complex" types. These numbers are
so large that I either get an overflow error, or some funky code like #INF
or 1.#INDj. However I really need these numbers to be calculated (although
precision isn't key). Is there a way to get python to increase the size
limit of float and complex numbers? I should mention that I'm using a lot of
pre-made modules and functions like math.exp() and scipy.special.erf() that
don't seem to be able to use available types like "Decimal" or "FixedPoint"
(especially since these don't seem to handle complex numbers).


Python floats are C doubles underneath, so you're stuck. You need extended
numeric types. Decimal is slow as molasses, and was not designed for
mathematical work (rather for finance-type fixed-point work). Use this
instead:

http://calcrpnpy.sourceforge.net/clnumManual.html

Cheers,

f

Sep 14 '05 #3
> "Todd Steury" <sc******@iwon.com> writes:
or 1.#INDj. However I really need these numbers to be calculated (although
precision isn't key). Is there a way to get python to increase the size
limit of float and complex numbers?

This is really a natural problem with such calculations.

On Wednesday 14 September 2005 02:30 pm, Paul Rubin wrote: You could rearrange your formulas to not need such big numbers:

x = 1000.
log10_z = x / math.log(10)
c,m = divmod(log10_z, 1.)
print 'z = %.5fE%d' % (10.**c, m)


I highly recommend you use this kind of solution. Solve the problem
with algebra, not with a new library. Most of the time, large numbers
can be avoided (particularly if you are not overly concerned with
precision), simply by dividing out large constant factors and the
like. Logs work better for this problem, as Paul points out.

--
Terry Hancock ( hancock at anansispaceworks.com )
Anansi Spaceworks http://www.anansispaceworks.com

Sep 14 '05 #4
Paul Rubin calculates exp(1000.0):
You could rearrange your formulas to not need such big numbers:

x = 1000.
log10_z = x / math.log(10)
c,m = divmod(log10_z, 1.)
print 'z = %.5fE%d' % (10.**c, m)


Nice approach. We should never forget that we do have mathematical
skills beside the computers. But, shouldn't you switch c and m in the
last row?

/MiO
Sep 15 '05 #5
Mikael Olofsson <mi****@isy.liu.se> writes:
print 'z = %.5fE%d' % (10.**c, m)


Nice approach. We should never forget that we do have mathematical
skills beside the computers. But, shouldn't you switch c and m in the
last row?


Yeah, doh. c=characteristic, m=mantissa.
Sep 15 '05 #6

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

Similar topics

4
by: Mike | last post by:
Related to another topic I just posted, I wanted to discuss ways to optimize the validation of very large (>100MB) XML documents. First, I have no idea if something like this already exists; it...
4
by: Jim West | last post by:
Both int a; std::complex<float> b; std::complex<float> c = static_cast<float>(a)*b; and int a; std::complex<float> b;
8
by: York | last post by:
Hi, R language has very high-level IO functions, its read.table can read a total .csv file and recogonize the types of each column. write.table can do the reverse. R's MySQL interface has...
24
by: Salad | last post by:
Every now and then I see ads that state something like "Experience with Large Databases ...multi-gig...blah-de-blah" And I have to laugh. What's the difference between a large or small database? ...
132
by: Frederick Gotham | last post by:
If we look at a programming language such as C++: When an updated Standard comes out, everyone adopts it and abandons the previous one. It seems though that things aren't so clear-cut in the C...
3
by: Kenneth McDonald | last post by:
Over the last couple of years, I've built a module called rex that lays on top of (and from the user's point of view, hides) the re module. rex offers the following advantages over re. *...
6
by: nishac | last post by:
Can anyone suggest me how to make my drop down menu work in IE7 too.Its working in other browsers.On mouse over the submenus should be displayed.Am attaching my css code hereby.Anyone please check...
16
by: DirtyHarry | last post by:
Good day everyone. This sounds like a stupid question, but I became just curious yesterday, and I looked up several textbooks. However, no textbooks on computer language (that I have ) mentioned...
2
by: =?Utf-8?B?c2lwcHl1Y29ubg==?= | last post by:
Have a complex process where I need to Import a large amount of data then run some transformations on this data then import into DataBase. The transformation involves multiple fields and multiple...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
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...
0
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...

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.