File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes pow() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "pow()" Watch "pow()" New topic


Monika Balogh

Joined: Aug 28, 2001
Posts: 26
I am trying to use the pow() method from the math library. I searched this forum for some help and this what I found on how to use it:
BigInteger bi = new BigInteger(x);
int result = bi.pow(y);
I taught when using this constructor x is a string, y has to be an integer. How can I use BigIntegers and integers with the pow() method?
What I tried to do is something like this:
BigInteger numGrains = new BigInteger("1");
int result = numGrains.pow(numGrains);
System.out.print(" " + result + " ");
This is the error message I recive when try to compile the class:
"pow(int) in java.math.BigInteger cannot be applied to (java.math
.BigInteger)int result = numGrains.pow(numGrains)"
I understand I'm passing the wrong argument type and when try to pass an integer I get this message:
incompatible types found : java.math.BigInteger
required: int
int result = numGrains.pow(2);
Is this not an int? I double checked it in Java API how to pass argument and cannot see the problem. I am confused.
Any help would be great!

Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

How about using the power function in the java.lang.Math package? I think it might be more useful, unless you're already using BigInteger for something else.

Should give you 100.0

If you want to use BigInteger, then watch out for what pow() returns.. another BigInteger, not an int.
This should give you 100

[This message has been edited by Mike Curwen (edited September 06, 2001).]
Monika Balogh

Joined: Aug 28, 2001
Posts: 26
Thank your help.
I used the BigInteger version and it worked.
I agree. Here's the link:
subject: pow()
It's not a secret anymore!