473,507 Members | 2,472 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Calucalting the power

hi,

i develeoped a recursive function for the power and it works fine

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n) {
  2. if (n < 0)
  3. return 1/power(x, -n);
  4. else if (n == 0)
  5. return 1.0;
  6. else
  7. return x * power(x, n-1);
  8. }
  9.  
But the problem is this function should have a runtime of log(n) and
therefore i should use this formula x^2n = x^n x^n in any way. does
maybe someone knows here how i can modifiy it to reach my aim...?

matti

Nov 3 '06 #1
5 6430
ma****@gmx.at wrote:
i develeoped a recursive function for the power and it works fine

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n) {
  2.  if (n < 0)
  3.    return 1/power(x, -n);
  4.  else if (n == 0)
  5.    return 1.0;
  6.  else
  7.    return x * power(x, n-1);
  8. }
  9.  

But the problem is this function should have a runtime of log(n) and
therefore i should use this formula x^2n = x^n x^n in any way. does
maybe someone knows here how i can modifiy it to reach my aim...?
If 'n' is even, calculate the power as (x^(n/2))*(x^(n/2))
If 'n' is odd, calculate the power as x*(x^(n/2))*(x^(n/2))

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '06 #2

so you mean in that way:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3. if (n==0)
  4. return 1.0;
  5.  
  6. if (0 == n%2)
  7. {
  8. return (x^((n-1)/2))*(x^((n-1)/2))
  9. }
  10. else
  11. {
  12. return x*(x^((n-1)/2))*(x^((n-1)/2))
  13. }
  14. }
  15.  
or did i interpret something wrong...?

matti

Nov 3 '06 #3
ma****@gmx.at wrote:
so you mean in that way:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3.    if (n==0)
  4.        return 1.0;
Expand|Select|Wrap|Line Numbers
  1.  
  2. You could also create a few other predefined return values,
  3. for example for 'n==1' or 'n==2', just to optimize a bit.
  4.  
  5.         
  6.                 >
  7.   if (0 == n%2)
  8.   {
  9.        return (x^((n-1)/2))*(x^((n-1)/2))
  10.   }
  11.    else
  12.   {
  13.       return x*(x^((n-1)/2))*(x^((n-1)/2))
  14.   }
  15. }
  16.  
  17.  
>
or did i interpret something wrong...?
Did I use "-1" anywhere in my reply (which you didn't quote)?
First of all, don't use ^, instead use the recursion which you
already have. Second, use -1 only where needed.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 3 '06 #4
Victor Bazarov wrote:
ma****@gmx.at wrote:
>so you mean in that way:

Expand|Select|Wrap|Line Numbers
  1. double power(double x, int n)
  2. {
  3.    if (n==0)
  4.        return 1.0;
Expand|Select|Wrap|Line Numbers
  1. You could also create a few other predefined return values,
  2. for example for 'n==1' or 'n==2', just to optimize a bit.
  3.         
  4.                 >  if (0 == n%2)
  5.   {
  6.        return (x^((n-1)/2))*(x^((n-1)/2))
  7.   }
  8.    else
  9.   {
  10.       return x*(x^((n-1)/2))*(x^((n-1)/2))
  11.   }
  12. }
  •  
  •  
  • >>
    or did i interpret something wrong...?

    Did I use "-1" anywhere in my reply (which you didn't quote)?
    First of all, don't use ^, instead use the recursion which you
    already have. Second, use -1 only where needed.
    And third, use recursion intelligently. You won't see any improvement
    in run time if you write, e.g.,

    return power(x,n/2) * power(x,n/2)

    because you're making two identical chains of recursive calls.
    Nov 3 '06 #5
    ma****@gmx.at wrote:
    so you mean in that way:

    [code]
    double power(double x, int n)
    {
    if (n==0)
    return 1.0;

    if (0 == n%2)
    {
    return (x^((n-1)/2))*(x^((n-1)/2))
    The ^ operator performs a bitwise exclusive or.

    In the standard C library, there is a function called pow for
    exponentiation.

    #include <cmath>

    double p = pow(x, n);
    or did i interpret something wrong...?
    I think Victor was just pulling your leg. It would be pretty dumb to
    call pow() twice on the halved exponent, and then multiply the two
    results together.

    // silly!
    double p = pow(x, n/2) * pow(x, n/2);

    Firstly, there are the extra divisions and multiplication. These not
    only add overhead but possible loss of precision due to truncation
    errors. Secondly, the compiler might not optimize the redundant call to
    pow away, and so the program may actually end up exponentiating twice.

    Nov 4 '06 #6

    This thread has been closed and replies have been disabled. Please start a new discussion.

    Similar topics

    6
    3974
    by: Stephane Belzile | last post by:
    Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
    12
    2823
    by: Roman Töngi | last post by:
    I just want to raise a number to a power. e.g.: 16^2 The pow()-function of cmath seems to be not enough. I've read about valarray. Is it really so difficult? Can you give me an example. ...
    0
    1586
    by: Thiva Charanasri | last post by:
    http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
    0
    1590
    by: Thiva Charanasri | last post by:
    http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
    1
    1209
    by: Richard Fennell | last post by:
    Am trying to do ASP.NET development on my XP Prof. box logged in as a power user (not the administrator I used to be). As an admin user all is OK. But as a power user, I have made sure my...
    9
    1781
    by: mathon | last post by:
    hi, i already posted an entry because of this problem, unfortunately i havent solved it so far..:( I have created a recursion for the calculation of the power like this: double...
    6
    2437
    by: maxthebaz | last post by:
    Our machines have this requirement: if power failure occurs, many important variables are to be resumed from where they were interrupted after the machine is restarted (power on in this case). In...
    3
    6953
    by: greek | last post by:
    the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
    8
    3088
    by: skanemupp | last post by:
    how do i solve power(5,1.3)? def power(nbr, po): if po==0: return 1 if po>0: return nbr*power(nbr, po-1) if po<0: return 1/power(nbr, -1*po)
    0
    7223
    marktang
    by: marktang | last post by:
    ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
    0
    7114
    by: Hystou | last post by:
    Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
    0
    7321
    Oralloy
    by: Oralloy | last post by:
    Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
    1
    7034
    by: Hystou | last post by:
    Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
    0
    4702
    by: conductexam | last post by:
    I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
    0
    3191
    by: TSSRALBI | last post by:
    Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
    0
    3179
    by: adsilva | last post by:
    A Windows Forms form does not have the event Unload, like VB6. What one acts like?
    1
    762
    muto222
    by: muto222 | last post by:
    How can i add a mobile payment intergratation into php mysql website.
    0
    412
    bsmnconsultancy
    by: bsmnconsultancy | last post by:
    In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

    By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

    To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.