As Fred Rosenberger has implied, there isn't an exponentiation operator as such, but if you stick to integers you can easily write a recursive algorithm to raise numbers to powers.

Hint: If the power is an even number, halve it and square the result. That way your algorithm will run in O(logn) time.

If you want to use small, positive, integer powers, just write the multiplication long-hand. For example, x to the power 3 is x*x*x. This is obvious to read and fast to execute. It applies to taking powers of integers and of floating-point values.

If you want to use possibly-large positive integer powers, do what Campbell suggests.

Negative, integer powers of floating-point numbers can be trivially done by taking the positive power and then doing 1.0/x at the end.

If you really need arbitrary, possibly fractional and possibly negative powers, use Math.pow(). Be aware that Math.pow() is fairly slow and is probably less accurate than the above methods, when the power is actually an integer. Of course, Math.pow() only applies to floating-point values, because you can't sensibly take an arbitrary power, in integer arithmetic.

Note that the above applies equally to most other languages that do have an exponentiation operator. Just replace "Math.pow()" by "the exponentiation operator". Only a few would be smart enough to optimise 2**3 as 2*2*2, for example. [ December 21, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.

Hmmm, sounds like overkill to me. Math.pow() is pretty fast. It's written in native code and, in the case that its arguments are integers, it can make use of the same O(log(N)) trick Campbell is talking about. The fact that it uses double rather than int or long is a mixed benefit - yes, it can be less precise in some cases. But in other cases it can be much, much more accurate, as it is much, much less likely to overflow. Add to this the benefit of being already written (and tested) for you.

Certainly, for low powers like 2 or 3, it's quick and simple to just write x*x or x*x*x. But for higher powers, I'd just use Math.pow() unless testing showed that this created a performance problem. And I say that as one who's already written and tested the O(log(N)) solution. In this day and age, it's pretty rare that a math operation is where your bottleneck will be anyway.

Incidentally, for polynomials, I'd often use Horner's rule anyway, which renders the question moot. [ December 21, 2007: Message edited by: Jim Yingst ]

"I'm not back." - Bill Harding, Twister

Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671

posted

0

[Prasanna]: /******* now if i want to pass the values from command line how do i change the code*********/

Well, have your read about how to use command-line arguments at all? Applying this tutorial to your problem seems straightforward. [ December 21, 2007: Message edited by: Jim Yingst ]

Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970

posted

0

Originally posted by Jim Yingst: Re: Campbell and Peter's posts:

Hmmm, sounds like overkill to me. Math.pow() is pretty fast.

I used to write programs for modelling chemical plants, often with rather under-powered hardware to play with. So I'm probably too keen on making maths go as fast as possible.

I certainly agree about how to do polynomials. Don't use Math.pow() for those.

Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107

22

posted

0

Originally posted by Jim Yingst: [QB]Re: Campbell and Peter's posts:

Hmmm, sounds like overkill to me. . .

Agree.

For real-life use, use x * x for square and Math.pow() for everything else.

For learning programming, you have to take a bit of overkill so as to see how algorithms work!