在我的程序中編碼矩陣乘法,我得到了精度誤差(大矩陣結果不準確)。矩陣乘法的精度誤差
這是我的代碼。當前對象將數據存儲在扁平數組中,並逐行排列。其他矩陣B的數據存儲在一個扁平數組中,列後列(所以我可以使用指針算術)。
protected double[,] multiply (IMatrix B)
{
int columns = B.columns;
int rows = Rows;
int size = Columns;
double[,] result = new double[rows,columns];
for (int row = 0; row < rows; row++)
{
for (int col = 0; col < columns; col++)
{
unsafe
{
fixed (float* ptrThis = data)
fixed (float* ptrB = B.Data)
{
float* mePtr = ptrThis + row*rows;
float* bPtr = ptrB + col*columns;
double value = 0.0;
for (int i = 0; i < size; i++)
{
value += *(mePtr++) * *(bPtr++);
}
result[row, col] = value;
}
}
}
}
}
實際上,該代碼是一個比較複雜:我做了幾個塊乘法東西(所以不是從0到大小有我的,我從localStart到localStop去),再總結產生的矩陣。
我的問題:一個大的矩陣我得到精度誤差:
NUnit.Framework.AssertionException: Error at (0,1)
expected: <6.4209571409444209E+18>
but was: <6.4207619776304906E+18>
任何想法?
也許使用'precision'標籤應該會自動打開關於在這些問題中經常出現的浮點數學的網頁? – Skizz 2010-04-27 13:20:36