二重型変数に関する多くの問題、等式のテストやゼロ除算などの問題が発生した後、二重値を処理するクラスを作成し、新しいクラスで定期的に使用する固有の二重をシームレスに切り替えようと考えました。しかし、それは完全には適合しません。これが私のクラスです:
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;
}
};
1つの問題は、たとえばcos()関数を呼び出す既存の関数のようなもので、doubleを予期します。必要なときにクラスオブジェクトを組み込みのdoubleに減衰させる方法はありますか?ありがとう。
ps私のクラスは既存のコードとシームレスに適合しなければなりません。それを変えることはできません。私にできることは、doubleを検索してHDoubleに置き換えることだけです。