hi,
i develeoped a recursive function for the power and it works fine -
double power(double x, int n) {
-
if (n < 0)
-
return 1/power(x, -n);
-
else if (n == 0)
-
return 1.0;
-
else
-
return x * power(x, n-1);
-
}
-
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 5 6450 ma****@gmx.at wrote:
i develeoped a recursive function for the power and it works fine
-
double power(double x, int n) {
-
if (n < 0)
-
return 1/power(x, -n);
-
else if (n == 0)
-
return 1.0;
-
else
-
return x * power(x, n-1);
-
}
-
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
so you mean in that way: -
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))
-
}
-
else
-
{
-
return x*(x^((n-1)/2))*(x^((n-1)/2))
-
}
-
}
-
or did i interpret something wrong...?
matti ma****@gmx.at wrote:
so you mean in that way:
-
double power(double x, int n)
-
{
-
if (n==0)
-
return 1.0;
-
-
You could also create a few other predefined return values,
-
for example for 'n==1' or 'n==2', just to optimize a bit.
-
-
-
>
-
if (0 == n%2)
-
{
-
return (x^((n-1)/2))*(x^((n-1)/2))
-
}
-
else
-
{
-
return x*(x^((n-1)/2))*(x^((n-1)/2))
-
}
-
}
-
-
-
>
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
Victor Bazarov wrote:
ma****@gmx.at wrote:
>so you mean in that way:
- double power(double x, int n)
- {
- if (n==0)
- return 1.0;
-
You could also create a few other predefined return values,
-
for example for 'n==1' or 'n==2', just to optimize a bit.
-
-
> if (0 == n%2)
- {
- return (x^((n-1)/2))*(x^((n-1)/2))
- }
- else
- {
- return x*(x^((n-1)/2))*(x^((n-1)/2))
- }
- }
-
-
-
>> 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. 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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
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
|
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.
Thanks Roman
|
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 very auspicious occasion, Thai people will join hands with
|
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 very auspicious occasion, Thai people will join hands with
|
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 account to the power user,
debugging user and vs developers and all seems OK bar debugging ASP.NET.
I get an access denied error that I cannot connect the debugger. The help
informs that it is probably due the ASPNET work process not being the right
| |
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 power(double x, int n) {
|
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 other words,
the basic idea is to keep a snapshot of the state machine before it is
interrupted.
The board is provided with:
- a 32-bit H8S/2633 Hitachi microprocessor;
- a battery-backed memory (BBM), where these variables are stored; BBM
area...
|
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
ive written the program it runs but im not having the correct values..here is the code:
#include<iostream.h>
#include<conio.h>
|
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)
|
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |