Results 1 to 5 of 5

Thread: How do computers work out powers?

  1. #1

    Thread Starter
    Addicted Member stefano5's Avatar
    Join Date
    Jul 2006
    Location
    London UK
    Posts
    131

    How do computers work out powers?

    I'm deep in debate with a collegue of mine as to how computers work out powers. i.e what is the working behind the Math.Pow() function?

    I seem to remember from school there being a method involving natural logarithms. Is this correct? And what is the working?

    Thanks

    Stefano
    We are continually faced with a series of great opportunities brilliantly disguised as insoluble problems.
    - John W. Gardner

  2. #2

    Thread Starter
    Addicted Member stefano5's Avatar
    Join Date
    Jul 2006
    Location
    London UK
    Posts
    131

    Re: How do computers work out powers?

    Ok so I think I've managed to jog my memory....

    x^n = e^(n*ln(x))

    since both e^x and ln(x) can be calculated using series this should work right?
    We are continually faced with a series of great opportunities brilliantly disguised as insoluble problems.
    - John W. Gardner

  3. #3
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: How do computers work out powers?

    You could use that. There are also series for x^n.

    However, computers often use Exponentiation by Squaring. One would compute 15^73 by taking [ignoring order of operations so I don't have to write a zillion parentheses]

    15^2^2^2*15^2^2^2*15 = 15^[(2*2*2+1)*2*2*2+1] = 15^[9*8+1] = 15^73.

    This reduces exponentiation to a few multiplications. I'm pretty sure you could generalize this without too much hassle to floating point numbers, taking the number of bits of precision multiplications per exponentiation.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

  4. #4

    Thread Starter
    Addicted Member stefano5's Avatar
    Join Date
    Jul 2006
    Location
    London UK
    Posts
    131

    Re: How do computers work out powers?

    That's very elegant, I guess it must be easier for computers to use any of the Exponentiation by Squaring methods if x and n are both Integer values as series often result in floating point numbers, and we all know what that means

    I'd be interested to see a Exponentiation by Squaring method which could be applied to non integer values.
    We are continually faced with a series of great opportunities brilliantly disguised as insoluble problems.
    - John W. Gardner

  5. #5
    Only Slightly Obsessive jemidiah's Avatar
    Join Date
    Apr 2002
    Posts
    2,431

    Re: How do computers work out powers?

    I believe the above method works fine if x is a floating point number, just using floating point multiplications at each step. You would want to make sure you don't under or overflow in intermediate steps.

    If n is a floating point number... I have no idea how to extend it.
    The time you enjoy wasting is not wasted time.
    Bertrand Russell

    <- Remember to rate posts you find helpful.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width