打印非常大的 BigIntegers [英] Printing very big BigIntegers
问题描述
我正在尝试找出与 Java 7 x64 中的 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());
}
}
我在打印此操作的结果时遇到问题.当此代码执行时,它会打印消息而不是 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()
.
为了隔离问题,我开始计算 2 的幂,而不是该行注释中列出的所需计算.在我测试过的两个系统上,2^15544
是触发问题的最小计算;2^15543
工作正常.
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.
我还没有达到主机系统上的内存限制,我不相信我什至接近 VM 限制(无论如何使用 VM 参数运行 -Xmx1024M
-Xms1024M
没有效果).
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).
在互联网上四处寻找答案后,我开始怀疑我在 BigInteger
或 String
中达到了与数组最大大小相关的限制(Integer.MAX_VALUE
),这些类型用于内部数据存储.如果问题出在 String
中,我认为可以扩展 BigInteger
并编写一个打印方法,一次喷出几个字符,直到整个 BigInteger
已打印,但我更怀疑问题出在其他地方.
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.
推荐答案
问题是Eclipse中Console视图的一个bug.
The problem is a bug of the Console view in Eclipse.
在我的设置中,Eclipse(Helios 和 Juno)无法在没有 CRLF 的情况下显示超过 4095 个字符的单行.最大长度可能因您选择的字体而异 - 见下文.
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.
因此,即使是以下代码也会显示问题 - 不需要 BigInteger
.
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.
作为一种解决方法,您可以在控制台首选项中设置 Fixed width console
设置,该字符串将立即出现:
As a workaround, you can set Fixed width console
setting in Console preferences, the string will immediatelly appear:
Eclipse 的 bugzilla 上对应的 bug 是:
The corresponding bugs on Eclipse's bugzilla are:
根据这些,这是一个 Windows/GTK 错误,Eclipse 的开发人员对此无能为力.
According to those, it's a Windows/GTK bug and Eclipse's developers can't do anything about it.
该bug与文本长度为像素有关,使用较小的字体,您将能够在它之前的文本中获得更多字符休息.
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.
这篇关于打印非常大的 BigIntegers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!