Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > 2 * 2 * 2 * 2 or pow(2, 4)?

Reply
Thread Tools

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

 
 
Olumide
Guest
Posts: n/a
 
      06-13-2008
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


 
Reply With Quote
 
 
 
 
Kai-Uwe Bux
Guest
Posts: n/a
 
      06-13-2008
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
 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      06-14-2008
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/>
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
255.255.255.255 redwaters Cisco 0 08-07-2007 04:43 AM
norton takes exception to 255.255.255.255 Phil Computer Security 3 02-20-2005 05:32 AM
IP changes to 255.255.255.255?? Joe Mama Computer Security 9 06-30-2004 03:42 AM
255.255.255.255 subnet mask Assignment Eazzy MCSE 8 07-25-2003 04:24 PM



Advertisments