x^y function?

  • Follow


How can I create a function that multiplicates x with x y times?
0
Reply hannibalkannibal (34) 9/12/2003 6:15:28 PM

Eirik Wix�e Svela <hannibalkannibal@yahoo.no> wrote:

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

Why not use the built in one? (Math.h - pow)

-- 
== Eric Gorr ========= http://www.ericgorr.net ========= ICQ:9293199 ===
"Therefore the considerations of the intelligent always include both 
benefit and harm." - Sun Tzu
== Insults, like violence, are the last refuge of the incompetent... ===
0
Reply egDfAusenetE5fz (78) 9/12/2003 7:53:02 PM


Eirik Wix�e Svela wrote:
> 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));
  }


-- 
Martin Ambuhl

0
Reply mambuhl (2201) 9/12/2003 8:02:37 PM

Eirik Wix�e Svela wrote:

> 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;
	  }

0
Reply E.Robert.Tisdale (2031) 9/12/2003 8:04:31 PM

"Eirik Wix�e Svela" <hannibalkannibal@yahoo.no> wrote in message
>
> 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.


0
Reply Malcolm 9/12/2003 8:18:26 PM


Eirik Wix�e Svela wrote:

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

Why not use "pow"? See http://www-ccs.ucsd.edu/c/math.html#pow

Regards,

	Ed.

0
Reply mortonAVOIDINGSPAM (167) 9/13/2003 1:43:27 AM

On Fri, 12 Sep 2003 13:04:31 -0700, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> wrote in comp.lang.c:

> Eirik Wix�e Svela wrote:
> 
> > 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;
> 	  }

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
0
Reply jackklein (3932) 9/14/2003 3:16:03 AM

On Fri, 12 Sep 2003 13:04:31 -0700, "E. Robert Tisdale"
<E.Robert.Tisdale@jpl.nasa.gov> wrote:

> Eirik Wix�e Svela wrote:
> 
> > 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;
> 	  }

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
0
Reply david.thompson1 (1042) 9/22/2003 2:50:30 AM

7 Replies
35 Views

(page loaded in 0.153 seconds)

Similiar Articles:













7/12/2012 4:04:23 PM


Reply: