2012-09-21 22 views
0

在遇到很多關於double類型變量的問題之後,像測試相等和除零等問題,我想到讓一個類處理double值並嘗試無縫切換我們使用的內在雙精度定期與新班上課。儘管如此,它並不適合。這裏是我的類:C++中的double類class

class HDouble 
{ 
    //private: 
public: 
    double dValue; 
    static const double dEpsilon; 


    HDouble() 
    { 
    dValue = 0.0; 
    } 

    HDouble(double OtherValue) 
    { 
    if (IsNaN(OtherValue)) 
    { 
     assert(0); 
    } 
    dValue = OtherValue; 
    } 

    const HDouble& operator=(const HDouble& OtherValue) 
    { 
    if (this == &OtherValue)  // Same object? 
     return *this; 

    if (IsNaN(OtherValue.dValue)) 
    { 
     assert(0); 
    } 
    dValue = OtherValue.dValue; 
    return *this; 
    } 

    const HDouble& operator=(const double& OtherValue) 
    { 
    dValue = OtherValue; 
    return *this; 
    } 
    bool operator==(const HDouble& OtherValue) 
    { 
    return (abs(dValue - OtherValue.dValue) < dEpsilon); 
    } 

    ////////////////////////////////////////////////////////////////////////// 
    const HDouble& operator++() 
    { 
    dValue++; 
    return *this; 
    } 
    const HDouble& operator++(int dummy) 
    { 
    dValue++; 
    return *this; 
    } 
    const HDouble& operator--() 
    { 
    dValue--; 
    return *this; 
    } 
    const HDouble& operator--(int dummy) 
    { 
    dValue--; 
    return *this; 
    } 


////////////////////////////////////////////////////////////////////////// 
    HDouble operator*(const HDouble& OtherValue) 
    { 
    HDouble Result = *this; 
    Result *= OtherValue; 
    return Result; 
    } 
    HDouble operator*(const double& OtherValue) 
    { 
    HDouble Result = *this; 
    Result *= OtherValue; 
    return Result; 
    } 

    HDouble operator/(const HDouble& OtherValue) 
    { 
    HDouble Result = *this; 
    Result /= OtherValue; 
    return Result; 
    } 
    HDouble operator/(const double& OtherValue) 
    { 
    HDouble Result = *this; 
    Result /= OtherValue; 
    return Result; 
    } 

    HDouble operator+(const HDouble& OtherValue) 
    { 
    HDouble Result = *this; 
    Result += OtherValue; 
    return Result; 
    } 
    HDouble operator+(const double& OtherValue) 
    { 
    HDouble Result = *this; 
    Result += OtherValue; 
    return Result; 
    } 

    HDouble operator-(const HDouble& OtherValue) 
    { 
    HDouble Result = *this; 
    Result -= OtherValue; 
    return Result; 
    } 
    HDouble operator-(const double& OtherValue) 
    { 
    HDouble Result = *this; 
    Result -= OtherValue; 
    return Result; 
    } 

    ////////////////////////////////////////////////////////////////////////// 
    HDouble& operator*=(const double& OtherValue) 
    { 
    dValue *= OtherValue; 
    return *this; 
    } 
    HDouble& operator*=(const HDouble& OtherValue) 
    { 
    dValue *= OtherValue.dValue; 
    return *this; 
    } 

    HDouble& operator+=(const HDouble& OtherValue) 
    { 
    dValue += OtherValue.dValue; 
    return *this; 
    } 
    HDouble& operator+=(const double& OtherValue) 
    { 
    dValue += OtherValue; 
    return *this; 
    } 


    HDouble& operator-=(const double& OtherValue) 
    { 
    dValue -= OtherValue; 
    return *this; 
    } 
    HDouble& operator-=(const HDouble& OtherValue) 
    { 
    dValue -= OtherValue.dValue; 
    return *this; 
    } 

    HDouble& operator/=(const double& OtherValue) 
    { 
    dValue /= OtherValue; 
    return *this; 
    } 
    HDouble& operator/=(const HDouble& OtherValue) 
    { 
    dValue /= OtherValue.dValue; 
    return *this; 
    } 
    ////////////////////////////////////////////////////////////////////////// 


    inline bool IsNaN(double d) 
    { 
    if (!(d >= DBL_MIN && d <= DBL_MAX)) 
    { 
     return true; 
    } 
    else 
     return false; 
    } 
}; 

一個問題,就像是已經存在的COS例如()函數調用的函數,它預計雙。有沒有辦法讓我的類對象在需要時衰減到內在的雙倍?謝謝。

p.s.我的課必須與現有代碼無縫配合。不能改變這一點。我所能做的就是搜索並用HDouble替換double。

+1

覆蓋演員:http://www.cplusplus.com/forum/general/25434/ – TJD

+4

類似這樣的類沒有機會無縫地代替double,因爲它打破了數學平等的基本規則:你的==不再是可傳遞的。當你看到'a == b'和'b == c'時,你不能暗示'a == c'。不幸的是,這是一切都崩潰的地方:沒有可靠的'=='運算符的類沒有用處。這個錯誤既是常見的,又是舊的:我記得在編寫Algol編譯器的過程中,讀到了Dijkstra關於犯同樣錯誤的說法。 – dasblinkenlight

+0

值得一提的是(未來,也許)自從C++ 11以來,您可以製作明確的轉換運算符,以避免發生隱式轉換。 – chris

回答

0

是的,請向您的班級添加轉化操作員:operator double() { return dValue; }。這樣,當你將它傳遞給cos時,你的HDouble將被轉換爲雙倍。

另外,您已將所有操作符實現爲成員函數。我建議閱讀關於 operator overloading

0

您可以添加一個方法返回double

double value() 
{ 
    return dValue; 
} 
0

你需要一個拷貝構造函數和轉換操作符添加到您的類,並且你也應該類的數據成員移到private

class HDouble 
{ 
private: 
    double dValue; 

public: 
    static const double dEpsilon; 

    HDouble() 
    { 
    dValue = 0.0; 
    } 

    HDouble(double OtherValue) 
    { 
    if (IsNaN(OtherValue)) 
    { 
     assert(0); 
    } 
    dValue = OtherValue; 
    } 

    HDouble(const HDouble &src) 
    { 
    dValue = src.dValue; 
    } 

    ... 

    operator double() const 
    { 
    return dValue; 
    } 

    ... 
};