**这是一个更正的帖子**
我正在使用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
**我认为原始数据中只有标签错误。
答案 0 :(得分:0)
这可能属于这里。
我要感谢所有关注这个问题的人。
我可能偶然发现了答案,这是我的问题,幸福。我在我的测试程序中发现了一个错误,修复它就解决了上面的问题。我真正追求的问题也消失了但是由于某种原因,诊断痕迹仍然打印出如上所述的不一致值,这仍然是一个谜。
整个问题似乎与继承类的静态初始化没有按照我想的顺序发生 - 在一个案例中。
答案 1 :(得分:0)
跟进。我错了。上述“答案”仅部分解决了测试程序中的问题。它在真正的程序中不起作用。
我意识到这是相当复杂的,但我确实找到了修正神秘不一致输出的修正,这应该证明是令人信服的:
I在使用toBigDecimal()[为错误的数据]之前添加了以下行:
x = new BigDecimal(x.toString());
我怀疑BigDecimal的某些属性已经损坏了。这不是规模。