Decimal and Exponentiation

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • elventear

    #1

    Decimal and Exponentiation

    Hi,

    I am the in the need to do some numerical calculations that involve
    real numbers that are larger than what the native float can handle.

    I've tried to use Decimal, but I've found one main obstacle that I
    don't know how to sort. I need to do exponentiation with real
    exponents, but it seems that Decimal does not support non integer
    exponents.

    I would appreciate if anyone could recommend a solution for this
    problem.

    Thank you.

  • Tim Peters

    #2
    Re: Decimal and Exponentiation

    [elventear][color=blue]
    > I am the in the need to do some numerical calculations that involve
    > real numbers that are larger than what the native float can handle.
    >
    > I've tried to use Decimal, but I've found one main obstacle that I
    > don't know how to sort. I need to do exponentiation with real
    > exponents, but it seems that Decimal does not support non integer
    > exponents.
    >
    > I would appreciate if anyone could recommend a solution for this
    > problem.[/color]

    Wait <0.3 wink>. Python's Decimal module intends to be a faithful
    implementation of IBM's proposed standard for decimal arithmetic:



    Last December, ln, log10, exp, and exponentiation to non-integral
    powers were added to the proposed standard, but nobody yet has written
    implementation code for Python's module. [Python-Dev: somebody
    wants to volunteer for this :-)]

    If you're not a numeric expert, I wouldn't recommend that you try this
    yourself (in particular, trying to implement x**y as exp(ln(x)*y)
    using the same precision is mathematically correct but is numerically
    badly naive).

    The GNU GMP library (for which Python bindings are available) also
    supports "big floats", but their power operation is also restricted to
    integer powers and/or exact roots. This can be painful even to try;
    e.g.,
    [color=blue][color=green][color=darkred]
    >>> from gmpy import mpf
    >>> mpf("1e10000") ** mpf("3.01")[/color][/color][/color]

    consumed well over a minute of CPU time (on a 3.4 GHz box) before dying with

    ValueError: mpq.pow fractional exponent, inexact-root

    If you're working with floats outside the range of IEEE double, you
    _probably_ want to be working with logarithms instead anyway; but that
    depends on you app, and I don't want to know about it ;-)

    Comment

    • Dan Bishop

      #3
      Re: Decimal and Exponentiation

      Tim Peters wrote:
      ....[color=blue]
      > Wait <0.3 wink>. Python's Decimal module intends to be a faithful
      > implementation of IBM's proposed standard for decimal arithmetic:
      >
      > http://www2.hursley.ibm.com/decimal/
      >
      > Last December, ln, log10, exp, and exponentiation to non-integral
      > powers were added to the proposed standard, but nobody yet has written
      > implementation code for Python's module. [Python-Dev: somebody
      > wants to volunteer for this :-)][/color]

      Here's a quick-and-dirty exp function:


      def exp(x):
      """
      Return e raised to the power of x.
      """
      if x < 0:
      return 1 / exp(-x)
      partial_sum = term = 1
      i = 1
      while True:
      term *= x / i
      new_sum = partial_sum + term
      if new_sum == partial_sum:
      return new_sum
      partial_sum = new_sum
      i += 1

      Comment

      • Raymond L. Buvel

        #4
        Re: Decimal and Exponentiation

        elventear wrote:[color=blue]
        > Hi,
        >
        > I am the in the need to do some numerical calculations that involve
        > real numbers that are larger than what the native float can handle.
        >
        > I've tried to use Decimal, but I've found one main obstacle that I
        > don't know how to sort. I need to do exponentiation with real
        > exponents, but it seems that Decimal does not support non integer
        > exponents.
        >
        > I would appreciate if anyone could recommend a solution for this
        > problem.
        >
        > Thank you.
        >[/color]
        The clnum module has arbitrary precision floating point and complex
        numbers with all of the standard math functions. For example, the cube
        root of 2 can be computed to 40 decimal places with the following.
        [color=blue][color=green][color=darkred]
        >>> from clnum import mpf,mpq
        >>> mpf(2,40)**mpq( 1,3)[/color][/color][/color]
        mpf('1.25992104 989487316476721 060727822835057 0251464701',46)

        For more information see



        Comment

        • Raymond L. Buvel

          #5
          Re: Decimal and Exponentiation

          Tim Peters wrote:
          <snip>
          [color=blue]
          > The GNU GMP library (for which Python bindings are available) also
          > supports "big floats", but their power operation is also restricted to
          > integer powers and/or exact roots. This can be painful even to try;
          > e.g.,
          >[color=green][color=darkred]
          > >>> from gmpy import mpf
          > >>> mpf("1e10000") ** mpf("3.01")[/color][/color]
          >
          > consumed well over a minute of CPU time (on a 3.4 GHz box) before dying
          > with
          >
          > ValueError: mpq.pow fractional exponent, inexact-root
          >[/color]
          <snip>

          The clnum module handles this calculation very quickly:
          [color=blue][color=green][color=darkred]
          >>> from clnum import mpf
          >>> mpf("1e10000") ** mpf("3.01")[/color][/color][/color]
          mpf('9.99999999 999999999999999 32861e30099',26 )[color=blue][color=green][color=darkred]
          >>> x=_
          >>> x ** (1/mpf("3.01"))[/color][/color][/color]
          mpf('9.99999999 999999999999999 53924e9999',26)

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

          Comment

          • Tim Peters

            #6
            Re: Decimal and Exponentiation

            [Raymond L. Buvel, on

            ][color=blue]
            > The clnum module handles this calculation very quickly:
            >[color=green][color=darkred]
            > >>> from clnum import mpf
            > >>> mpf("1e10000") ** mpf("3.01")[/color][/color]
            > mpf('9.99999999 999999999999999 32861e30099',26 )[/color]

            That's probably good enough for the OP's needs -- thanks!

            OTOH, it's not good enough for the decimal module:

            (10**10000)**3. 01 =
            10**(10000*3.01 ) =
            10**30100

            exactly, and the proposed IBM standard for decimal arithmetic requires
            error < 1 ULP (which implies that if the mathematical ("infinite
            precision") result is exactly representable, then that's the result
            you have to get). It would take some analysis to figure out how much
            of clnum's error is due to using binary floats instead of decimal, and
            how much due to its pow implementation.

            Comment

            • Raymond L. Buvel

              #7
              Re: Decimal and Exponentiation

              Tim Peters wrote:[color=blue]
              > [Raymond L. Buvel, on
              > http://calcrpnpy.sourceforge.net/clnumManual.html
              > ]
              >[color=green]
              >> The clnum module handles this calculation very quickly:
              >>[color=darkred]
              >> >>> from clnum import mpf
              >> >>> mpf("1e10000") ** mpf("3.01")[/color]
              >> mpf('9.99999999 999999999999999 32861e30099',26 )[/color]
              >
              >
              > That's probably good enough for the OP's needs -- thanks!
              >
              > OTOH, it's not good enough for the decimal module:
              >
              > (10**10000)**3. 01 =
              > 10**(10000*3.01 ) =
              > 10**30100
              >
              > exactly, and the proposed IBM standard for decimal arithmetic requires
              > error < 1 ULP (which implies that if the mathematical ("infinite
              > precision") result is exactly representable, then that's the result
              > you have to get). It would take some analysis to figure out how much
              > of clnum's error is due to using binary floats instead of decimal, and
              > how much due to its pow implementation.[/color]

              Indeed, it is not clear where the error is comming from especially since
              you can increase the precision of the intermediate calculation and get
              the exact result.
              [color=blue][color=green][color=darkred]
              >>> mpf(mpf("1e1000 0",30) ** mpf("3.01",30), 20)[/color][/color][/color]
              mpf('1.0e30100' ,26)

              Is this the kind of thing you will need to do in the Decimal module to
              meet the specification?

              Comment

              • Tim Peters

                #8
                Re: Decimal and Exponentiation

                [Raymond L. Buvel, on

                ][color=blue][color=green][color=darkred]
                >>> The clnum module handles this calculation very quickly:
                >>>
                >>> >>> from clnum import mpf
                >>> >>> mpf("1e10000") ** mpf("3.01")
                >>> mpf('9.99999999 999999999999999 32861e30099',26 )[/color][/color][/color]

                [Tim Peters][color=blue][color=green]
                >> That's probably good enough for the OP's needs -- thanks!
                >>
                >> OTOH, it's not good enough for the decimal module:
                >>
                >> (10**10000)**3. 01 =
                >> 10**(10000*3.01 ) =
                >> 10**30100
                >>
                >> exactly, and the proposed IBM standard for decimal arithmetic requires
                >> error < 1 ULP (which implies that if the mathematical ("infinite
                >> precision") result is exactly representable, then that's the result
                >> you have to get). It would take some analysis to figure out how much
                >> of clnum's error is due to using binary floats instead of decimal, and
                >> how much due to its pow implementation.[/color][/color]

                [Raymond][color=blue]
                > Indeed, it is not clear where the error is comming from especially since
                > you can increase the precision of the intermediate calculation and get
                > the exact result.
                >[color=green][color=darkred]
                > >>> mpf(mpf("1e1000 0",30) ** mpf("3.01",30), 20)[/color][/color]
                > mpf('1.0e30100' ,26)
                >
                > Is this the kind of thing you will need to do in the Decimal module to
                > meet the specification?[/color]

                It will vary by function and the amount of effort people are willing
                to put into implementations . Temporarily (inside a function's
                implementation) increasing working precision is probably the easiest
                way to get results provably suffering less than 1 ULP error in the
                destination precision. The "provably" part is the hardest part under
                any approach ;-)

                Comment

                Working...