gpt4 book ai didi

java - 有谁知道这是否是 Java 库错误?

转载 作者:太空宇宙 更新时间:2023-11-04 14:05:52 26 4
gpt4 key购买 nike

**这是一个更正的帖子**

我正在使用 BigDecimal 进行一些非常复杂的数学计算,并在数千个测试之一中遇到了错误。有人看到我做错了什么愚蠢的事情吗?我不这么认为。

下面的输出(根据原始帖子更正)是计算得出的几条痕迹之一。似乎 Java 只是错误地打印非 BigInteger 变量,因为该函数使用 x、ix、y 和 iy 工作。问题是:为什么 x 的值会从一次打印到下一次打印发生变化? - 这似乎不是我应该做的事情。

当我不小心将内部常量 pi 和 e 精确到小数点后 1,500 位时,发生了错误。在 1,000 个地方,一切都很好,没有错误。

转储中的值 x 恰好 = 2*PI,约为 6.283185307179586476925286766559 下面是代码片段。

public  static  int     cmp(BigDecimal x, BigDecimal y, int places)
{
BigInteger ix = x.movePointRight(places).toBigInteger();

String sInt = ix.toString();
BigDecimal shiftX = x.movePointRight(places);
String sx = x.movePointRight(places).toString();
int dot = sx.indexOf('.'); // making the shifted x
if (dot > 0) // string into an integer string
sx = sx.substring(0,dot); // just for comparison
if ( !sx.equals(sInt) )
{
System.out.println("****** cmp(): Mismatch between X values. dec places = " + places);
System.out.println("x = " + x);
System.out.println("x.toString() = " + x.toString());
System.out.println("x.toPlain() = " + x.toPlainString());

System.out.println("x.right() #1 = " + x.movePointRight(places));
System.out.println("x.right() #2 = " + shiftX);
System.out.println("x.right() #3 = " + sx);

String shiftXStr = x.movePointRight(places).toString();
System.out.println("x.right().str() #1 = " + x.movePointRight(places).toString());
System.out.println("x.right().str() #2 = " + shiftXStr);

String shiftXPlain = x.movePointRight(places).toPlainString();
System.out.println("x.right().plain() 1 = " + x.movePointRight(places).toPlainString());
System.out.println("x.right().plain() 2 = " + shiftXPlain);

System.out.println("x.toBigInt() = " + x.toBigInteger());

System.out.println("BigInt(x) #1 = " + x.movePointRight(places).toBigInteger());
System.out.println("BigInt(x) #2 = " + ix);

System.out.println("BigInt(x).str() 1 = " + x.movePointRight(places).toBigInteger().toString());
System.out.println("BigInt(x).str() 2 = " + sInt);
}

输出是:(只有最后一行和从它开始的 2 行是正确的。请注意,错误的值始终是正确值的 2^n 的倍数,包括为简洁起见而未显示的测试 - 为了便于阅读,我在右侧截断了)

******  cmp(): Mismatch between X values.    dec places = 595
x = 205887.41614566068967588779676660550101874569
x.toString() = 205887.41614566068967588779676660550101874569
x.toPlain() = 205887.41614566068967588779676660550101874569
x.right() #1 = 205887416145660689675887796766605501018745693
x.right() #2 = 205887416145660689675887796766605501018745693
x.right() #3 = 205887416145660689675887796766605501018745693
x.right().str() #1 = 205887416145660689675887796766605501018745693
x.right().str() #2 = 205887416145660689675887796766605501018745693
x.right().plain() 1 = 205887416145660689675887796766605501018745693
x.right().plain() 2 = 205887416145660689675887796766605501018745693
x.toBigInt() = 205887
BigInt(x) #1 = 205887416145660689675887796766605501018745693
BigInt(x) #2 = 628318530717958647692528676655900576839433879
BigInt(x).str() 1 = 205887416145660689675887796766605501018745693
BigInt(x).str() 2 = 628318530717958647692528676655900576839433879

** 我相信这只是原始数据中的标签错误。

最佳答案

这可能属于这里。

我必须对所有关注这个问题的人表示感谢。

我可能偶然发现了答案,很高兴这是我的问题。我在我的测试程序中发现了一个错误,修复它可以解决上述问题。我真正想要的问题也消失了,但由于某种原因,诊断跟踪仍然打印出与上面不一致的值,这仍然是一个谜。

整个问题似乎与继承类的静态初始化没有按照我想的顺序发生有关 - 在一种情况下。

关于java - 有谁知道这是否是 Java 库错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28878857/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com