Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exponential operator in java?

 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is there any operator which works as a exponential operator??

suppose i pass 2, 3 at command line i should get 8

similarly 3 2 is 9
 
fred rosenberger
lowercase baba
Bartender
Posts: 12081
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
take a look at the api for the Math class.
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

CR
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
pras
Ranch Hand
Posts: 188
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi guys,
this is the program i wrote.

class Power {

public static void main(String args[]) {

double a=Math.pow(2,3);
System.out.println(a);
}
}

/******* now if i want to pass the values from command line how do i change the code*********/
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Re: Campbell and Peter's posts:

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 ]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[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
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic