Convert Rational relational functions to operator overrides

This commit is contained in:
Josh Koon 2019-02-21 11:50:30 -08:00
commit 0d298e13f3
5 changed files with 42 additions and 65 deletions

View file

@ -389,96 +389,70 @@ namespace CalcEngine
return this->P().IsZero();
}
bool Rational::IsLess(Rational const& r, int32_t precision) const
bool operator==(Rational const& lhs, Rational const& rhs)
{
PRAT thisRat = this->ToPRAT();
PRAT rRat = r.ToPRAT();
PRAT lhsRat = lhs.ToPRAT();
PRAT rhsRat = rhs.ToPRAT();
bool result = false;
try
{
result = rat_lt(thisRat, rRat, precision);
result = rat_equ(lhsRat, rhsRat, RATIONAL_PRECISION);
}
catch (DWORD error)
{
destroyrat(thisRat);
destroyrat(rRat);
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error);
}
destroyrat(thisRat);
destroyrat(rRat);
destroyrat(lhsRat);
destroyrat(rhsRat);
return result;
}
bool Rational::IsLessEq(Rational const& r, int32_t precision) const
bool operator!=(Rational const& lhs, Rational const& rhs)
{
PRAT thisRat = this->ToPRAT();
PRAT rRat = r.ToPRAT();
return !(lhs == rhs);
}
bool operator<(Rational const& lhs, Rational const& rhs)
{
PRAT lhsRat = lhs.ToPRAT();
PRAT rhsRat = rhs.ToPRAT();
bool result = false;
try
{
result = rat_le(thisRat, rRat, precision);
result = rat_lt(lhsRat, rhsRat, RATIONAL_PRECISION);
}
catch (DWORD error)
{
destroyrat(thisRat);
destroyrat(rRat);
destroyrat(lhsRat);
destroyrat(rhsRat);
throw(error);
}
destroyrat(thisRat);
destroyrat(rRat);
destroyrat(lhsRat);
destroyrat(rhsRat);
return result;
}
bool Rational::IsGreaterEq(Rational const& r, int32_t precision) const
bool operator>(Rational const& lhs, Rational const& rhs)
{
PRAT thisRat = this->ToPRAT();
PRAT rRat = r.ToPRAT();
bool result = false;
try
{
result = rat_ge(thisRat, rRat, precision);
}
catch (DWORD error)
{
destroyrat(thisRat);
destroyrat(rRat);
throw(error);
}
destroyrat(thisRat);
destroyrat(rRat);
return result;
return rhs < lhs;
}
bool Rational::IsEq(Rational const& r, int32_t precision) const
bool operator<=(Rational const& lhs, Rational const& rhs)
{
PRAT thisRat = this->ToPRAT();
PRAT rRat = r.ToPRAT();
return !(lhs > rhs);
}
bool result = false;
try
{
result = rat_equ(thisRat, rRat, precision);
}
catch (DWORD error)
{
destroyrat(thisRat);
destroyrat(rRat);
throw(error);
}
destroyrat(thisRat);
destroyrat(rRat);
return result;
bool operator>=(Rational const& lhs, Rational const& rhs)
{
return !(lhs < rhs);
}
wstring Rational::ToString(uint32_t radix, NUMOBJ_FMT fmt, int32_t precision) const

View file

@ -1002,9 +1002,9 @@ int CCalcEngine::IdcSetAngleTypeDecMode(int idc)
bool CCalcEngine::IsCurrentTooBigForTrig()
{
if (m_currentVal.IsGreaterEq(m_maxTrigonometricNum, m_precision))
if (m_currentVal >= m_maxTrigonometricNum)
{
m_currentVal = Rational{};
m_currentVal = 0;
return true;
}

View file

@ -61,7 +61,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
// Can be converting a dec negative number to Hex/Oct/Bin rep. Use 2's complement form
// Check the range.
if (result.IsLess(0, m_precision))
if (result < 0)
{
// if negative make positive by doing a twos complement
result = -(result) - 1;
@ -83,7 +83,7 @@ void CCalcEngine::DisplayNum(void)
// called.
//
if (m_bRecord ||
!gldPrevious.value.IsEq(m_currentVal, m_precision) ||
gldPrevious.value != m_currentVal ||
gldPrevious.precision != m_precision ||
gldPrevious.radix != m_radix ||
gldPrevious.nFE != (int)m_nFE ||

View file

@ -31,7 +31,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
case IDC_RSHF:
{
if (m_fIntegerMode && result.IsGreaterEq(Rational{ m_dwWordBitWidth }, m_precision)) // Lsh/Rsh >= than current word size is always 0
if (m_fIntegerMode && result >= m_dwWordBitWidth) // Lsh/Rsh >= than current word size is always 0
{
throw CALC_E_NORESULT;
}
@ -55,7 +55,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
}
case IDC_LSHF:
if (m_fIntegerMode && result.IsGreaterEq(Rational{ m_dwWordBitWidth }, m_precision)) // Lsh/Rsh >= than current word size is always 0
if (m_fIntegerMode && result >= m_dwWordBitWidth) // Lsh/Rsh >= than current word size is always 0
{
throw CALC_E_NORESULT;
}

View file

@ -59,10 +59,13 @@ namespace CalcEngine
Rational Not(Rational const& chopNum, int32_t precision) const;
bool IsZero() const;
bool IsLess(Rational const& r, int32_t precision) const;
bool IsLessEq(Rational const& r, int32_t precision) const;
bool IsGreaterEq(Rational const& r, int32_t precision) const;
bool IsEq(Rational const& r, int32_t precision) const;
friend bool operator==(Rational const& lhs, Rational const& rhs);
friend bool operator!=(Rational const& lhs, Rational const& rhs);
friend bool operator<(Rational const& lhs, Rational const& rhs);
friend bool operator>(Rational const& lhs, Rational const& rhs);
friend bool operator<=(Rational const& lhs, Rational const& rhs);
friend bool operator>=(Rational const& lhs, Rational const& rhs);
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
uint64_t ToUInt64_t(int32_t precision) const;