osg3.6.3平臺,其中 Matrixd 類,判定相等代碼如下:
bool operator == (const Matrixd& m) const { return compare(m)==0; }
int Matrix_implementation::compare(const Matrix_implementation& m) const
{
const Matrix_implementation::value_type* lhs = reinterpret_cast<const Matrix_implementation::value_type*>(_mat);
const Matrix_implementation::value_type* end_lhs = lhs+16;
const Matrix_implementation::value_type* rhs = reinterpret_cast<const Matrix_implementation::value_type*>(m._mat);
for(;lhs!=end_lhs;++lhs,++rhs)
{
if (*lhs < *rhs) return -1;
if (*rhs < *lhs) return 1;
}
return 0;
}
//其中 typedef float value_type;
我記得double型別,比較相等是下面這樣的啊?
if(d1<d2 + tolerance && d1>d2 - tolerance)
其中tolerance是容差,我設定為了 tolerance=0.000001;
所以,上面那個osg原始碼是什么情況?
uj5u.com熱心網友回復:
*lhs和*rhs的型別是Matrix_implementation::value_type,不是你以為的double況且就算你實際使用時,將Matrix_implementation::value_type定義為double
那也可以多載operator <,并在其具體實作中使用你所說的tolerance
要學會在IDE中,滑鼠右鍵點你不明白的符號,選轉到定義。
uj5u.com熱心網友回復:
還有也許用==判斷兩個Matrixd的實體是否相等,目的是判斷一個向另一個賦值時,通過呼叫拷貝建構式復制了一份;
而不是用來判斷一個是期望的計算結果,另一個是通過很多步驟算出來的結果,兩者是否在滿足一定tolerance范圍內近似相等(由于計算機浮點數表示在計算后有不可避免的誤差:浮點數格式 http://bbs.csdn.net/topics/390676437)。
如果你實際需要判斷是否在滿足一定tolerance范圍內近似相等,可以使用現有的或自己另撰寫一個比如
int isApproximateEequal(Matrixd &a,Matrixd &b);

轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/285264.html
標籤:C++ 語言
上一篇:定時器關閉不生效問題