我在VS8專業版上使用C#。我有一段代碼,我最初是在Pascal中編寫的,然後是C編寫的,因此C#直到現在才被使用。代碼片段看起來是這樣的: -在VS8中找到兩個double值的錯誤C#
*bool InverseTransverseMercator(coord enl)
{
double nu, rho, k0;
double x, y, x2, x3, x4, x5, x6, x7;
double phif, phi, cosphif, sinphif, lam, n, n2, n3, n4;
double t, t2, t3, t4, t6, nu2, nu3, nu5, nu7, neta2;
double a0, a1, a2, a3, p7, p8, p9, p10, p11, p12, p12a;
coord cc; //has a structure public double x, y, z;
cc = enl;
x = cc.x;
y = cc.y;
x2 = x * x;
x3 = x * x2;//I stop the debugger at the start of this line and examine some values.
...*
下面是調試器的值是無我真的很困惑: -
x = 23500.0 - original value
x * x = 552250000.0 (right hand side of x2=x * x correct)
x2 = 552249984.0 (left hand side of x2 = x * x INCORRECT!)
正如你可以看到x * x
值是正確的但在價值它被分配的變量(X2)不是。事實上,答案相差16個。 這些都是雙重變量,我期望在第16位有效數字四捨五入。我嘗試使用'decimal'類型使用Math.Pow(x,2)
重命名變量,並且都有問題,x2
的值不是它應該是的。我嘗試重新排列線條,並添加更多虛擬線條而沒有區別。通過我的代碼搜索我沒有全球性的名稱x2
。有趣的是,這個值確實會變成接近確切的答案,就好像一個巨大的舍入誤差正在完成一樣。我必須在這裏做一些愚蠢而愚蠢的事情,但我看不到它。我非常感謝有人就此問題提出的任何建議。
請修復格式。 – Timwi 2010-08-08 14:40:11