Reputation:
I have working with really big number in java. I'm using BigInteger. My number in hex is 0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff
It is about this in decimal:
1.1579208921035625e+77
The code:
public class BigNumber
{
public static void main(String[] args)
{
BigInteger fp = new BigInteger(0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff);
}
}
And I get error:
BigNumber.java:5: error: integer number too large: ffffffff00000001000000000000000000000000ffffffffffffffffffffffff
BigInteger fp = new BigInteger(0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff);
How can I use this big number without loosing the data? Is this possible ? I thought that BigInteger is only limited by memory.
Upvotes: 3
Views: 584
Reputation: 23349
The literal 0xfff..
is an integer literal. The compiler detects overflows in literals and this results in a compile error.
You can put it in a string and specify the radix to BigDecimal
BigInteger fp = new BigInteger("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff",16);
And btw, a constructor that takes an integer as a parameter doesn't exist, but the compiler chose to return this error message.
Upvotes: 2
Reputation: 82511
Since you don't use a String
to contain the data, the java compiler considers it an int
and handles it accordingly. It's obviously too large for the type int
:
Use
new BigInteger("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", 16)
instead.
Upvotes: 4
Reputation: 137309
That's because you need to use the String
constructor, that also takes a radix. In this case, the radix is 16.
public static void main(String[] args) {
BigInteger fp = new BigInteger("ffffffff00000001000000000000000000000000ffffffffffffffffffffffff", 16);
System.out.println(fp);
}
In your snippet, the hexadecimal value is converted into an int
because it is an integer literal and since it is too big to fit in an int
, an error is raised.
Upvotes: 10