Print very large BigIntegers

I am trying to figure out the following problem related to BigIntegers in Java 7 x64. I am trying to calculate a number to extremely high power. The code below is followed by a description of the problem.

import java.math.BigInteger; public class main { public static void main(String[] args) { // Demo calculation; Desired calculation: BigInteger("4096").pow(800*600) BigInteger images = new BigInteger("2").pow(15544); System.out.println( "The number of possible 16 bpc color 800x600 images is: " + images.toString()); } } 

I'm having trouble printing the result of this operation. When this code executes this, it prints a message, but not the value of images.toString() .

To isolate the problem, I began to calculate the powers of the two instead of the desired calculation, listed in the comment on this line. On two systems I tested this, 2^15544 - the smallest calculation that causes the problem; 2^15543 working fine.

I donโ€™t know where close to the memory limit on the host systems, and I donโ€™t think that I am even close to the VM limit (in any case, with the arguments of VM -Xmx1024M -Xms1024M there is no effect).

After searching for information on the Internet, I came to the conclusion that I click on the limit in BigInteger or String associated with the maximum array size ( Integer.MAX_VALUE ) that these types use for internal data storage. If the problem is in String , I think it would be possible to extend BigInteger and write a print method that spews a few characters at a time until all BigInteger is printed, but I rather suspect the problem is in another place.

Thanks for taking the time to read my question.

+7
source share
1 answer

The problem is the console submission error in Eclipse.

In my setup, Eclipse (Helios and Juno) cannot display a single line longer than 4095 characters without CRLF. The maximum length may vary depending on your choice of font - see below.

Therefore, even the following code will show the problem - there is no need for BigInteger .

 StringBuilder str = new StringBuilder(); for (int i = 0; i < 4096; i++) { str.append('?'); } System.out.println(str); 

However, the line is actually printed on the console - you can, for example, copy it from it. It is simply not shown.

As a workaround, you can set the Fixed width console parameter in the console settings, the line will be displayed immediately:

view of the pref

Relevant errors in eclipse bugzilla:

This is said to be a Windows / GTK bug, and Eclipse developers cannot do anything about it.

The error is related to the length of the text - these are pixels, use a smaller font, and you can get more characters in the text before this breaks.

+11
source

All Articles