2012-09-21 11 views
0

double 타입 변수에 관한 문제, 평등과 0으로 나누기 테스트와 같은 문제가 많은데 이중 값을 처리하고 우리가 사용하는 본질적인 double을 완벽하게 전환하려고하는 클래스를 만들려고 생각했습니다. 새로운 반원들과 정기적으로 그것은 완전히 맞지 않습니다. 여기 내 수업이 있습니다 :더블 타입 클래스 (C++)

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() 함수와 같습니다. 예를 들어 double이 필요합니다. 내 클래스 객체가 필요할 때 본질적인 이중으로 썩는 방법이 있습니까? 감사.

p.s. 내 수업은 기존 코드와 원활하게 맞아야합니다. 그럴 수 없어. 내가 할 수있는 일은 검색과 이중을 HDouble로 바꾸는 것뿐입니다.

+1

캐스팅을 재정의합니다. http://www.cplusplus.com/forum/general/25434/ – TJD

+4

클래스는 수학 평등의 근본적인 규칙을 위반하기 때문에 double 대신에 매끄럽게 끼울 확률이 0입니다. '=='는 더 이상 전이 적이 지 않습니다. 'a == b'와'b == c'를 볼 때'a == c'를 암시 할 수 없습니다. 안타깝게도, 이것이 모두 고장이났다. 신뢰할 수있는'=='연산자가없는 클래스는 쓸모가 없다. 이 실수는 일반적이고 오래된 것입니다. 나는 Algol 컴파일러를 작성하는 맥락에서 같은 실수를 범한 Dijkstra의 이야기를 읽었던 것을 기억합니다. – dasblinkenlight

+0

C++ 11 이후에는 암시 적 변환이 일어나지 않도록 명시 적 변환 연산자를 만들 수 있다는 것을 언급하는 것이 좋습니다 (미래에는 아마도). – chris

답변

0

예, 변환 연산자를 클래스에 operator double() { return dValue; } 클래스에 추가하십시오. 그러면 HDoublecos에 전달하면 double로 변환됩니다.

또한 모든 연산자를 멤버 함수로 구현했습니다. 나는 operator overloading에 관하여 독서를 건의 할 것입니다.

당신은 double 반환하는 방법을 추가 할 수 있습니다
0

: 당신은 당신의 클래스에 복사 생성자 및 변환 연산자를 추가해야합니다, 당신은 또한 private에 클래스의 데이터 멤버를 이동해야

double value() 
{ 
    return dValue; 
} 
0

:

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; 
    } 

    ... 
};