# Exponential operator in java?

pras

Ranch Hand

Posts: 188

posted 8 years ago

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

Posts: 48652

56

posted 8 years ago

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(log

CR

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

*n*) time.CR

Peter Chase

Ranch Hand

Posts: 1970

posted 8 years ago

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 ]

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

Posts: 188

posted 8 years ago

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*********/

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

Sheriff

Posts: 18671

posted 8 years ago

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 ]

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

Sheriff

Posts: 18671

posted 8 years ago

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 ]

**[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 ]

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

Peter Chase

Ranch Hand

Posts: 1970

posted 8 years ago

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.

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.

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

Campbell Ritchie

Sheriff

Posts: 48652

56

posted 8 years ago

For real-life use, use

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

Agree.Originally posted by Jim Yingst:

[QB]Re: Campbell and Peter's posts:

Hmmm, sounds like overkill to me. . .

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!