2 * 2 * 2 * 2 or pow(2, 4)?

Discussion in 'C++' started by Olumide, Jun 13, 2008.

  1. Olumide

    Olumide Guest

    Hi ,

    I hope this isn't too trivial to ask, but I'm evaluating a polynomial
    that involve lots of terms like x^2y^2z^3, and although I can use the
    power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for
    example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3).
    Or is it all the same?

    Thanks,

    - Olumide
     
    Olumide, Jun 13, 2008
    #1
    1. Advertising

  2. Olumide

    Kai-Uwe Bux Guest

    Olumide wrote:

    > I hope this isn't too trivial to ask, but I'm evaluating a polynomial
    > that involve lots of terms like x^2y^2z^3, and although I can use the
    > power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for
    > example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3).
    > Or is it all the same?


    There is no way to tell a priory. You have to measure.

    However, if your polynomial has "lots of terms" like those, you might want
    to try the Horner scheme of evaluating polynomials:

    a_5 x^5 + a_4 x^4 + a_3 x^3 + a_2 x^2 + a_1 x^1 + a_0

    can be rewritten as

    ( ( ( ( a_5 * x + a_4 ) * x + a_3 ) * x + a_2 ) * x + a_1 ) * x + a_0

    which uses only 5 multiplications and 5 additions.


    Best

    Kai-Uwe Bux
     
    Kai-Uwe Bux, Jun 13, 2008
    #2
    1. Advertising

  3. Olumide

    Daniel Pitts Guest

    Kai-Uwe Bux wrote:
    > Olumide wrote:
    >
    >> I hope this isn't too trivial to ask, but I'm evaluating a polynomial
    >> that involve lots of terms like x^2y^2z^3, and although I can use the
    >> power function pow(a, b), I wonder if just writing x*x*y*y*z*z*z for
    >> example wouldn't be more efficient than pow(x, 2)*pow(y, 2)*pow(z,3).
    >> Or is it all the same?

    >
    > There is no way to tell a priory. You have to measure.
    >
    > However, if your polynomial has "lots of terms" like those, you might want
    > to try the Horner scheme of evaluating polynomials:
    >
    > a_5 x^5 + a_4 x^4 + a_3 x^3 + a_2 x^2 + a_1 x^1 + a_0
    >
    > can be rewritten as
    >
    > ( ( ( ( a_5 * x + a_4 ) * x + a_3 ) * x + a_2 ) * x + a_1 ) * x + a_0
    >
    > which uses only 5 multiplications and 5 additions.
    >
    >
    > Best
    >
    > Kai-Uwe Bux

    Which is also an easy way to convert a string representation of a number
    into the number itself :)

    --
    Daniel Pitts' Tech Blog: <http://virtualinfinity.net/wordpress/>
     
    Daniel Pitts, Jun 14, 2008
    #3
    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. Michael B. Williams

    Math.pow Question

    Michael B. Williams, May 29, 2004, in forum: Java
    Replies:
    3
    Views:
    4,816
    Chris Smith
    May 29, 2004
  2. Michael B. Williams

    (re;) Math.pow Question

    Michael B. Williams, May 30, 2004, in forum: Java
    Replies:
    1
    Views:
    382
    Sudsy
    May 30, 2004
  3. Michael B. Williams

    for Mr Andrew Thompson re: Math.pow Question

    Michael B. Williams, May 30, 2004, in forum: Java
    Replies:
    1
    Views:
    342
    Andrew Thompson
    May 30, 2004
  4. Clueless Moron

    math.pow vs pow

    Clueless Moron, Nov 27, 2003, in forum: Python
    Replies:
    5
    Views:
    927
    John J. Lee
    Nov 28, 2003
  5. Michel Rouzic

    pow(2, 1/2) != pow(2, 0.5) problem

    Michel Rouzic, Jun 15, 2005, in forum: C Programming
    Replies:
    52
    Views:
    1,672
    Alan Balmer
    Jun 20, 2005
Loading...

Share This Page