Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
415 views
in Technique[技术] by (71.8m points)

java - Printing very big BigIntegers

I'm trying to figure out the following issue related to BigIntegers in Java 7 x64. I am attempting to calculate a number to an extremely high power. Code is below, 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 am encountering issues printing the result of this operation. When this code executes it prints the message but not the value of images.toString().

To isolate the problem I started calculating powers of two instead of the desired calculation listed in the comment on that line. On the two systems I have tested this on, 2^15544 is the smallest calculation that triggers the problem; 2^15543 works fine.

I'm no where close to hitting the memory limit on the host systems and I don't believe that I am even close to the VM limit (at any rate running with the VM arguments -Xmx1024M -Xms1024M has no effect).

After poking around the internet looking for answers I have come to suspect that I am hitting a limit in either BigInteger or String related to the maximum size of an array (Integer.MAX_VALUE) that those 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 out a few chars at a time until the entire BigInteger is printed, but I rather suspect that the problem lies elsewhere.

Thank you for taking the time to read my question.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The problem is a bug of the Console view in Eclipse.

On my setup, Eclipse (Helios and Juno) can't show a single line longer than 4095 characters without CRLF. The maximum length can vary depending on your font choice - see below.

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

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

That said, the string is actually printed in the console - you can for instance copy it out of it. It is just not shown.

As a workaround, you can set Fixed width console setting in Console preferences, the string will immediatelly appear:

view of the pref

The corresponding bugs on Eclipse's bugzilla are:

According to those, it's a Windows/GTK bug and Eclipse's developers can't do anything about it.

The bug is related to the length of the text is pixels, use a smaller font and you will be able to get more characters in the text before it breaks.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...