Skip to main content
broken link fixed
Source Link
Glorfindel
  • 22.6k
  • 13
  • 89
  • 116

This page describes an algorithm: http://mathforum.org/library/drmath/view/55896.htmlLink. x^(1/n) = the nth root of x, and x^mn = (x^m)^n. Thus, x^(m/n) = (the nth root of x)^m. Arbitrary roots can be calculated with Newton's method. Integer powers can be calculated with Exponentiation by squaring. For irrational exponents, you can use increasingly accurate rational approximations until you get the desired number of significant digits.

This page describes an algorithm: http://mathforum.org/library/drmath/view/55896.html. x^(1/n) = the nth root of x, and x^mn = (x^m)^n. Thus, x^(m/n) = (the nth root of x)^m. Arbitrary roots can be calculated with Newton's method. Integer powers can be calculated with Exponentiation by squaring. For irrational exponents, you can use increasingly accurate rational approximations until you get the desired number of significant digits.

This page describes an algorithm: Link. x^(1/n) = the nth root of x, and x^mn = (x^m)^n. Thus, x^(m/n) = (the nth root of x)^m. Arbitrary roots can be calculated with Newton's method. Integer powers can be calculated with Exponentiation by squaring. For irrational exponents, you can use increasingly accurate rational approximations until you get the desired number of significant digits.

replaced http://stackoverflow.com/ with https://stackoverflow.com/
Source Link
URL Rewriter Bot
URL Rewriter Bot
added 1561 characters in body
Source Link
wombat57
  • 1.3k
  • 1
  • 13
  • 26

EDIT 1: Here are links to the actual source

IsEDIT 2:

Newton's method involves raising your current guess to the power of the root that you're trying to find. If that power is large, and the guess is even a little too high, this what you already tried? Whycan result in overflow. One solution here is it causingto identify this case. If overflow? ever occurs, this means that the guess was too high. You can solve the problem by (whenever a guess results in overflow), setting the current guess to a value between the last guess that did not overflow and the current guess (you may have to do this several times). That is, whenever Newton's method overflows, do a binary search down toward the last guess that did not overflow. Here's some python that implements all of this:

def nroot(n, b, sig_figs = 10):
    g1 = 1.0
    g2 = 1.0
    while True:
        done = False
        while not done:  
            try:
                g3 = g2 - ((g2**b) - n) / (b * (g2**(b-1)))
                done = True
            except OverflowError:
                g2 = (g1 + g2) / 2.0 

        if abs(g2 - g3) < 1.0 / (10**sig_figs):
            return g3
        g1 = g2
        g2 = g3

def npowbysqr(n, p):
    if p == 0:
        return 1.0
    if p % 2 == 0:
        v = npowbysqr(n, p/2)
        return v*v 
    else:
        return n*npowbysqr(n, p-1)

def npow(n, p):
    return npowbysqr(nroot(n, 1000000), int(p*1000000))

print npow(5, 4.3467)
print 5**4.3467   
         

I should add that there are probably much better solutions. This does seem to work, however

EDIT: Here are links to the actual source

Is this what you already tried? Why is it causing overflow?

EDIT 1: Here are links to the actual source

EDIT 2:

Newton's method involves raising your current guess to the power of the root that you're trying to find. If that power is large, and the guess is even a little too high, this can result in overflow. One solution here is to identify this case. If overflow ever occurs, this means that the guess was too high. You can solve the problem by (whenever a guess results in overflow), setting the current guess to a value between the last guess that did not overflow and the current guess (you may have to do this several times). That is, whenever Newton's method overflows, do a binary search down toward the last guess that did not overflow. Here's some python that implements all of this:

def nroot(n, b, sig_figs = 10):
    g1 = 1.0
    g2 = 1.0
    while True:
        done = False
        while not done:  
            try:
                g3 = g2 - ((g2**b) - n) / (b * (g2**(b-1)))
                done = True
            except OverflowError:
                g2 = (g1 + g2) / 2.0 

        if abs(g2 - g3) < 1.0 / (10**sig_figs):
            return g3
        g1 = g2
        g2 = g3

def npowbysqr(n, p):
    if p == 0:
        return 1.0
    if p % 2 == 0:
        v = npowbysqr(n, p/2)
        return v*v 
    else:
        return n*npowbysqr(n, p-1)

def npow(n, p):
    return npowbysqr(nroot(n, 1000000), int(p*1000000))

print npow(5, 4.3467)
print 5**4.3467   
         

I should add that there are probably much better solutions. This does seem to work, however

added 342 characters in body
Source Link
wombat57
  • 1.3k
  • 1
  • 13
  • 26
Loading
Source Link
wombat57
  • 1.3k
  • 1
  • 13
  • 26
Loading