473,466 Members | 1,378 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

power - novice

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
Jul 23 '05 #1
12 2820
"Roman Töngi" <ro**********@bluewin.ch> writes:
I just want to raise a number to a power.
e.g.:
16^2
The pow()-function of cmath seems to be not enough. In what way "not enough"? What went wrong?
I've read about valarray. Is it really so difficult?

Is this question related to the pow() one?

Jul 23 '05 #2

"Tim Love" <tp*@eng.cam.ac.uk> schrieb im Newsbeitrag
news:d2**********@gemini.csx.cam.ac.uk...
"Roman Töngi" <ro**********@bluewin.ch> writes:
I just want to raise a number to a power.
e.g.:
16^2
The pow()-function of cmath seems to be not enough.

In what way "not enough"? What went wrong?
I've read about valarray. Is it really so difficult?

Is this question related to the pow() one?


I only know pow() of math.h.
Just tell me please the easiest way (for a beginner) how to raise for
example 16 to the power of 2.
Jul 23 '05 #3
Roman Töngi wrote:
[..]
I only know pow() of math.h.
Should be enough for your purposes.
Just tell me please the easiest way (for a beginner) how to raise for
example 16 to the power of 2.


pow(16,2)

Is this a trick question? Didn't you just say that you *knew* 'pow'?
Jul 23 '05 #4
Ooh sorry, I was absent.
thank you
Jul 23 '05 #5
On 5/4/05 7:16 AM, Roman Töngi wrote:
I just want to raise a number to a power.
e.g.:
16^2
The pow()-function of cmath seems to be not enough.


Could you do it manually? eg. 16*16. Or write a recursive function
that will do it for any arbitrary exponent.

And for powers of 2, how about using a shift function? 16 << 1.
Jul 23 '05 #6
Richard Cavell wrote:
On 5/4/05 7:16 AM, Roman Töngi wrote:
I just want to raise a number to a power.
e.g.:
16^2
The pow()-function of cmath seems to be not enough.


Could you do it manually? eg. 16*16. Or write a recursive function
that will do it for any arbitrary exponent.

And for powers of 2, how about using a shift function? 16 << 1.


You're confusing it with multiplying by a power of 2. 16 << 1 is 32.
16 * 16 is the same as 16 << 4.

V
Jul 23 '05 #7
I've got two problems with those that are weirdly sorta opposite each
other.

Or write a recursive function that will do it for any arbitrary
exponent.

I would recommend against recursion. A recursive algorithm, though it's
easy to do and understand, will be grossly inefficient for the problem.
An iterative one is just as easy:

template<typename T>
T exponentiate(T base, int exp)
{
T ret = 1;
for( ; exp>0 ; --exp)
ret *= base;
return ret;
}

It's very possible that your compiler will be able to convert your
recursive solution to an iterative one, but this is a fairly
substantial optimization at little cost, and do you really want to
trust that your compiler will recognize that this is optimizable? (I
just tried it under gcc under Solaris and I don't know enough Sun
assembly to know if it did.)
And for powers of 2, how about using a shift function? 16 << 1.


On the other hand, this seems like the sort of optimization that you
DON'T want to do. 16 << 1 is not that much faster than 16 * 2. Unless
you're doing it a whole lot or running in a very very very tight
environment it won't make any difference. Second, as much as it will
help, I will almost gurantee that the compiler is much more likely to
optimize 16 << 1 than it is to find tail recursion. This is the sort of
optimization that can probably be implemented even in debug builds.
Third, you've reduced readability. Maybe not much for something like 'x
= 16 << 1', but quick, what is the value of '3 << 1 + 1'? Is it 7 ('(3
<< 1) + 1') or 12 ('3 << (1+1)')? I bet you had to think about that
much more than 3 * 1 + 1. Oh, and it's a different answer. So you can't
just substitute '<<1' everywhere you see '*2'. (MSVC warns about it:
"warning C4554: '<<' : check operator precedence for possible error;
use parentheses to clarify precedence".)

Jul 23 '05 #8
Evan wrote:
I've got two problems with those that are weirdly sorta opposite each
other.

Or write a recursive function that will do it for any arbitrary
exponent.

I would recommend against recursion. A recursive algorithm, though
it's easy to do and understand, will be grossly inefficient for the
problem. An iterative one is just as easy:

template<typename T>
T exponentiate(T base, int exp)
{
T ret = 1;
for( ; exp>0 ; --exp)
ret *= base;
return ret;
}


Have you thought of

template<unsigned power, class T> T exponentiate(T base)
{
return base * exponentiate<power-1>(base);
}

template<> double exponentiate<1,double>(double d)
{
return d;
}

template<> int exponentiate<1>(int i)
{
return i;
}

? It could be even faster than your loop... Check this out:
--------------
template<unsigned power, class T> T exponentiate(T base)
{
return base * exponentiate<power-1>(base);
}

template<> double exponentiate<1,double>(double d)
{
return d;
}

template<> int exponentiate<1>(int i)
{
return i;
}

template<class T> T exponenti8(T d, unsigned p) {
T ret = 1;
for (; p > 0; p--)
ret *= d;
return ret;
}

#include <iostream>
#include <ctime>

int main()
{
double *d = new double[1000000];
std::clock_t t1 = clock();
for (int i = 0; i < 1000000; ++i)
d[i] = exponentiate<64>(2.0 + 0.000001 * i);
std::clock_t t2 = clock();
for (int i = 0; i < 1000000; ++i)
d[i] = exponenti8(2.0 + 0.000001 * i, 64);
std::clock_t t3 = clock();
delete[] d;

std::cout << t2-t1 << std::endl;
std::cout << t3-t2 << std::endl;
}
--------------
On my system it prints
187
563
--------------
which means templates are three times faster! That's the power of
inlining. Recursion rules, but only in compile-time. :-)

It's very possible that your compiler will be able to convert your
recursive solution to an iterative one, but this is a fairly
substantial optimization at little cost, and do you really want to
trust that your compiler will recognize that this is optimizable? (I
just tried it under gcc under Solaris and I don't know enough Sun
assembly to know if it did.)
[...]


V
Jul 23 '05 #9
On Mon, 04 Apr 2005 17:35:01 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
Roman Töngi wrote:
[..]
I only know pow() of math.h.


Should be enough for your purposes.
Just tell me please the easiest way (for a beginner) how to raise for
example 16 to the power of 2.


pow(16,2)

Is this a trick question? Didn't you just say that you *knew* 'pow'?


I get an error about ambiguous overloading of pow(), it's defined for

float, float
float, int
double, double
double, int
long double, long double
long double, int

(and if I include header <complex> for complex types as well) but not
for int, int.

Chris C
Jul 23 '05 #10
Chris Croughton wrote:
On Mon, 04 Apr 2005 17:35:01 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:

Roman Töngi wrote:
[..]
I only know pow() of math.h.


Should be enough for your purposes.

Just tell me please the easiest way (for a beginner) how to raise for
example 16 to the power of 2.


pow(16,2)

Is this a trick question? Didn't you just say that you *knew* 'pow'?

I get an error about ambiguous overloading of pow(),


If you put a dot after 16, does it help?
Jul 23 '05 #11
On 5/4/05 12:07 PM, Evan wrote:
And for powers of 2, how about using a shift function? 16 << 1.
On the other hand, this seems like the sort of optimization that you
DON'T want to do. 16 << 1 is not that much faster than 16 * 2. Unless
you're doing it a whole lot or running in a very very very tight
environment it won't make any difference.


For my purposes, 16 << 1 more closely describes what it is that my
algorithm is doing than 16 * 2.
"warning C4554: '<<' : check operator precedence for possible error;
use parentheses to clarify precedence".)


Yeah, the operator precedence is a trap. ( When I ( write ( C++
expressions ) ) I ( ( use parantheses ) everywhere ) because of this ).
Jul 23 '05 #12
On Tue, 05 Apr 2005 11:19:18 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
Chris Croughton wrote:
On Mon, 04 Apr 2005 17:35:01 -0400, Victor Bazarov
<v.********@comAcast.net> wrote:
Roman Töngi wrote:

[..]
I only know pow() of math.h.

Should be enough for your purposes.

Just tell me please the easiest way (for a beginner) how to raise for
example 16 to the power of 2.

pow(16,2)

Is this a trick question? Didn't you just say that you *knew* 'pow'?


I get an error about ambiguous overloading of pow(),


If you put a dot after 16, does it help?


Yup. My point was that pow(16, 2) will give errors, it's a "trick
answer".

(Why they didn't have pow(int, int) as well, I don't know. It seems a
pretty obvious thing to have if you have all of the floating point
versions and the lack of it is going to cause errors...)

Chris C
Jul 23 '05 #13

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

Similar topics

5
by: Marian | last post by:
Hi, I am totaly novice in .NET and I am studying a book about this. There was mentioned "assembly". I did not understand, how function does it has . I would like to know the exact run of code...
4
by: Tim Bird | last post by:
Hi all. I have recently installed VB2005 so teach myself programming, could anyone suggest any links to useful websites, or help sites, Ideally I am looking for tutorials, written for the...
6
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
0
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
by: gretchen.ogrady | last post by:
I admit - I'm a simple user but looking to improve skills. Instructions aren't helping and have searched this group but am getting bogged down by some of the programming-speak. I have a query...
0
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...
9
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...
3
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
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
jinu1996
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...
1
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
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,...
0
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
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
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 ...

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.