posted 10 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 offbyone errors
posted 10 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(logn) time.
CR
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
posted 10 years ago
If you want to use small, positive, integer powers, just write the multiplication longhand. 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 floatingpoint values.
If you want to use possiblylarge positive integer powers, do what Campbell suggests.
Negative, integer powers of floatingpoint 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 floatingpoint 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 possiblylarge positive integer powers, do what Campbell suggests.
Negative, integer powers of floatingpoint 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 floatingpoint 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 10 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*********/
posted 10 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 10 years ago
[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 commandline arguments at all? Applying this tutorial to your problem seems straightforward.
[ December 21, 2007: Message edited by: Jim Yingst ]
Well, have your read about how to use commandline 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 10 years ago
I used to write programs for modelling chemical plants, often with rather underpowered 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 underpowered 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
Marshal
Posts: 57499
175
posted 10 years ago
For reallife 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!
Agree.Originally posted by Jim Yingst:
[QB]Re: Campbell and Peter's posts:
Hmmm, sounds like overkill to me. . .
For reallife 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!
Try 100 things. 2 will work out, but you will never know in advance which 2. This tiny ad might be one:
The WEB SERVICES and JAXRS Course
https://coderanch.com/t/690789/WEBSERVICESJAXRS
