打印非常大的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视图的错误。
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.
作为一种解决方法,您可以在控制台偏好设置中设置固定宽度控制台
设置,字符串将立即出现:
As a workaround, you can set Fixed width console
setting in Console preferences, the string will immediatelly appear:
Eclipse的bugzilla上的相应错误是:
The corresponding bugs on Eclipse's bugzilla are:
- Display problem in console when a line reaches 4096 characters
- Texteditor can't show a line with more than 4095 chars. Limit at 4096 chars.
- Long lines are not displayed by editor
根据这些,这是一个Windows / GTK错误,Eclipse的开发人员可以'做任何事情。
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.
这篇关于打印非常大的BigIntegers的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!