• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problems with BigInteger

 
Richard Gibson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
in assignment 1.6:
I can create the list, and the final number for square 64 is
-9223372036854775808
So I am assuming I need to use the BigInteger class from the java.math.* package to get rid of that "-".
I have tried various combinations but to no avail. For example I tried using the method valueOf(long val) as follows:
public static BigInteger valueOf(long x)
{
return x;
};
in my "Grains" class and got the reply:
"Incompatible type for return. can't convert long to java.math.BigInteger."
which is what I thought this method was for!
I also looked at the pow(int exponent) method which looks like it could do all the work except that I too am lost with the "this" part. Can someone point me on the right track? Have I even chosen the right package?!
thanks,
Richard
 
Ray Marsh
Ranch Hand
Posts: 458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you want to use the BigInteger class you need to create an instance of it.
Example: BigInteger bi = new BigInteger
Then use the methods of BigInteger on that object.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In this case valueOf is a static method if BigInteger, so no instance is necessary.
Richard- if I understand what you're asking and doing, then you're creating your own definition of valueOf. This is unnecessary as the method is already defined in BigInteger to do what you want. You can use it like this:
<pre> BigInteger bi = BigInteger.valueOf(someValue);</pre>
where someValue can be any integral type (it will be implicitly converted to long if necessary).
 
Richard Gibson
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have managed to get BigInteger to work in the following code:

STUFF DELETED
which works (i.e. it compiles), but the result is still the -9.... number above. Is the problem that I need to do my multiplications directly with BigInteger. I tried the bi = (bi * 2) and, of course, got the message that BigInteger is NOT an int.
R

[This message has been edited by Paul Wheaton (edited March 21, 2000).]
 
paul wheaton
Trailboss
Pie
Posts: 21735
Firefox Browser IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I deleted some code that might give future cattle drive people too much info...
(I would answer questions, but my boss wants me and I have to go.)
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic