-3

I need to calculate much larger numbers than a long integer can store (example 1234567890*1234). Is there any way to surpass that memory capacity?

Pajk
  • 1
  • 1

3 Answers3

0

Check out BigInteger. Here is an example.

BigInteger v = BigInteger.valueOf(1234567890);
BigInteger prod = v.multiply(BigInteger.valueOf(1234));
System.out.println(prod);

If the numbers don't fit into a long, the supply them with a String.

v = new BigInteger("4994949234567890");
prod = v.multiply(new BigInteger("1234920292029292292"));
System.out.println(prod);

Prints

1523456776260
6168364167424068724128019127703880

The floating point counterpart is BigDecimal

WJS
  • 36,363
  • 4
  • 24
  • 39
-1

You can use with :

BigInteger num= new BigInteger("1234567890*1234");
Pluto
  • 853
  • 1
  • 8
  • 28
  • 3
    `BigInteger` won't take an arithmatic expression for its constructor, you would need to `new BigInteger("1234567890").multiply(BigInteger.valueOf(1234))` – Rogue Jun 10 '20 at 17:24
-1

If you want to stick with pure Java you can use BigInteger, but you could use the jscience. It's a great library with tons of optimizations for those kinds of problems.

Maybe this class, LargeInteger, will be good for you.

Here is an example for you:

LargeInteger dividend = LargeInteger.valueOf("3133861182986538201");
LargeInteger divisor = LargeInteger.valueOf("25147325102501733369");
Rational rational = Rational.valueOf(dividend, divisor);
System.out.println("rational  = " + rational);

> rational  = 41/329