After having a lot of problems regarding to double type variables, problems like testing for equality and division by zero, I thought of making a class to handle double values and try to seamlessly switch the intrinsic doubles we use regularly with the new class. it doesn't fit completely though. here's my 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;
}
};
one problem is like a function that already exist calls cos() function for example and it expects a double. is there a way for my class object to decay to an intrinsic double when needed? thanks.
p.s. my class must fit seamlessly with the existing code. cant change that. all i can do is search and replace double with HDouble.