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: how to solve symbolic function with initial values - comp.soft-sys ...hi, i have a symbolic equation and i want to solve with initial values which is x=1.2, y=1.2 syms x y; u = -x^2 + x - y + 0.75; v = x^2 - 5*x*... find y of x - comp.soft-sys.matlabHi, I have plotted a graph, and need to find the y-value for a given x-value. How exactly do I do that? I have tried using the "find"-function, bu... how we plot two dimensional dirac delta function - comp.soft-sys ...hi..... i am trying to plot a dirac delta function in a meshgrid of x,y. i want a delta function at (x=1,y=2)&(x=-3,y=1) then what is command for i... Curvature of a 3D implicit function - comp.soft-sys.matlab ...Hi, Given an implicit function f (x,y,z) = 0 , is there a formula that I can use to calculate the curvature at a point? Yours, Weng ... Help with Bootstrapping Function - comp.soft-sys.matlabMy question concerns the output of the bootstrp function. The bootstrap function is as follows: BOOTSTAT = BOOTSTRP(NBOOT,BOOTFUN,D1,...) If BO... Help in lsqr / fit - comp.soft-sys.matlabA function F has 2 parameters which I have to calculate. they are x and y in the function defined. f is the value of the function I have. This is the ... How to evaluate the Laplacian of a function as a function? - comp ...Hi, I have a stupid problem and cannot find a solution anywhere. I have the function func[x_,y_] := 3/Pi*(1 - x^2 + y^2))^2 which I need the Laplacian of ... Help with discrete double integral - comp.soft-sys.matlab ...Hi everyone, My problem is : I want to integrate f(x,y)*q(x) over x~(a,b),y~(c,d). f(x,y) and q(x) are discrete numbers not functions. lsqcurvefit z(x,y) - comp.soft-sys.matlabThree Stage Least Squares with Non-linear Restrictions - comp.soft ... lsqcurvefit z(x,y) - comp.soft-sys.matlab I am currently at this stage: Function file ... Different color plots in a loop - comp.soft-sys.matlab... legend('alpha=0.1','alpha=0.3','alpha=0.5','alpha=0.7','alpha=0.9','alpha=1.1',2) title('Corresponding Curves For Various Alpha Values Of The Function y=erf(alpha*x)') Function (mathematics) - Wikipedia, the free encyclopediaIn mathematics, a function is a relation between a set of inputs and a set of potential outputs with the property that each input is related to exactly one output. FunctionsLinks. Formula Sheet. Contact the Author. Terms & Disclaimer. Maths Notation. Functions Introduction. The phrase "y is a function of x" means that the value of y ... 7/12/2012 4:04:23 PM
|