File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Exponential operator in java? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Exponential operator in java?" Watch "Exponential operator in java?" New topic
Author

Exponential operator in java?

pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
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

Joined: Oct 02, 2003
Posts: 11357
    
  16

take a look at the api for the Math class.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39100
    
  23
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

Joined: Oct 30, 2001
Posts: 1970
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.
pras
Ranch Hand

Joined: Apr 04, 2007
Posts: 188
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

Joined: Jan 30, 2000
Posts: 18671
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 ]

"I'm not back." - Bill Harding, Twister
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
[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
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: 39100
    
  23
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!
 
GeeCON Prague 2014
 
subject: Exponential operator in java?