**这是修正后**
我正在使用的BigDecimal一些非常复杂的数学计算和几千测试ONE遇到了一个错误。 有谁知道我做错了什么傻事? 我不认为如此。
以下的输出(从原始交校正)是从计算几个轨迹中的一个。 看来,如果单纯的Java只打印非BigInteger的变量不正确,因为函数工作-用X,九,y和IY。 现在的问题是:为什么X的变化从一个打印到下一个价值? - 它似乎并不喜欢的事,我应该能够做到。
在发生错误时,我不小心做内部常量PI和电子精确到1500位小数。 在1000米的地方,一切正常,没有错误。
转储中的值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
**我相信这是只是错在原始数据的标签。