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

Discussion in 'Python' started by Todd Steury, Sep 14, 2005.

  1. Todd Steury

    Todd Steury Guest

    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
     
    Todd Steury, Sep 14, 2005
    #1
    1. Advertising

  2. Todd Steury

    Paul Rubin Guest

    "Todd Steury" <> 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)
     
    Paul Rubin, Sep 14, 2005
    #2
    1. Advertising

  3. 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
     
    Fernando Perez, Sep 14, 2005
    #3
  4. > "Todd Steury" <> 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
     
    Terry Hancock, Sep 15, 2005
    #4
  5. 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
     
    Mikael Olofsson, Sep 15, 2005
    #5
  6. Todd Steury

    Paul Rubin Guest

    Mikael Olofsson <> 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.
     
    Paul Rubin, Sep 15, 2005
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Soren Kuula
    Replies:
    2
    Views:
    564
    Henry S. Thompson
    Dec 1, 2005
  2. bd
    Replies:
    0
    Views:
    634
  3. Raymond Arthur St. Marie II of III

    very Very VERY dumb Question About The new Set( ) 's

    Raymond Arthur St. Marie II of III, Jul 23, 2003, in forum: Python
    Replies:
    4
    Views:
    483
    Raymond Hettinger
    Jul 27, 2003
  4. Carsten Fuchs
    Replies:
    45
    Views:
    1,554
    James Kanze
    Oct 8, 2009
  5. bmm
    Replies:
    0
    Views:
    142
Loading...

Share This Page