x^y function?

  • Thread starter =?iso-8859-1?q?Eirik_Wix=F8e_Svela?=
  • Start date
?

=?iso-8859-1?q?Eirik_Wix=F8e_Svela?=

How can I create a function that multiplicates x with x y times?
 
M

Martin Ambuhl

Eirik said:
How can I create a function that multiplicates x with x y times?

If you mean x raised to the y+1 power (x*x multiplies x by x one time),
just use
#include <math.h>
[...]
{ double x /* = initial value */;
double y /* = initial value */;
double z;
z = pow(x,y+1);
}

If you mean x raised to the (2 to the y power) power, from feeding
the last computed value into x,
#include <math.h>
[...]
{ double x /* = initial value */;
double y /* = initial value */;
double z;
z = pow(x,pow(2,y));
}
 
E

E. Robert Tisdale

Eirik said:
How can I create a function that multiplicates x with x y times?

int pow(int x, unsigned int y) {
return (0 < y)? (1 < y)? (pow(x, y/2)*pow(x, y - y/2): x): 1;
}
 
M

Malcolm

Eirik Wixøe Svela said:
How can I create a function that multiplicates x with x y times?
If y is a positive integer, simply use a for loop.

If you mean "how do I implement the pow() function" then the answer is that
it is probably the most complicated of the math library functions. There's
an implementation in Plauger's The Standard C Library. To actually
understand it you will need someone to go through the fundamental maths with
you.
 
J

Jack Klein

int pow(int x, unsigned int y) {
return (0 < y)? (1 < y)? (pow(x, y/2)*pow(x, y - y/2): x): 1;
}

Pure undefined behavior, of course. The standard specifically forbids
naming anything with external linkage the same as a standard library
function, even if the corresponding header is not included.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c++/faq
 
D

Dave Thompson

int pow(int x, unsigned int y) {
return (0 < y)? (1 < y)? (pow(x, y/2)*pow(x, y - y/2): x): 1;
}

The external name pow is reserved for the standard library function,
even if you don't call it; use something else (I like ipow).

The question as stated actually asks for x * pi(y times) x which is
ipow(x, y+1), but I agree was probably meant to be ipow(x, y).

The iterative form avoids any recursion overhead, and is IMHO clearer
to most people. But if you really want recursive, and care about
exponents large enough to be noticeable, which is actually quite
unlikely on any real system for nontrivial bases without overflowing,
square-and-multiply is better:

int ipow (int x, unsigned y) {
return y==0? 1: y==1? x: (y%2? x: 1) * ipow(x*x, y/2);
}

Which can also be made iterative, nearly as clearly.

- David.Thompson1 at worldnet.att.net
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top