mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-24 06:55:19 -07:00
Convert Rational relational functions to operator overrides
This commit is contained in:
parent
2fecd35fd2
commit
0d298e13f3
5 changed files with 42 additions and 65 deletions
|
@ -389,96 +389,70 @@ namespace CalcEngine
|
||||||
return this->P().IsZero();
|
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 lhsRat = lhs.ToPRAT();
|
||||||
PRAT rRat = r.ToPRAT();
|
PRAT rhsRat = rhs.ToPRAT();
|
||||||
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = rat_lt(thisRat, rRat, precision);
|
result = rat_equ(lhsRat, rhsRat, RATIONAL_PRECISION);
|
||||||
}
|
}
|
||||||
catch (DWORD error)
|
catch (DWORD error)
|
||||||
{
|
{
|
||||||
destroyrat(thisRat);
|
destroyrat(lhsRat);
|
||||||
destroyrat(rRat);
|
destroyrat(rhsRat);
|
||||||
throw(error);
|
throw(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyrat(thisRat);
|
destroyrat(lhsRat);
|
||||||
destroyrat(rRat);
|
destroyrat(rhsRat);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rational::IsLessEq(Rational const& r, int32_t precision) const
|
bool operator!=(Rational const& lhs, Rational const& rhs)
|
||||||
{
|
{
|
||||||
PRAT thisRat = this->ToPRAT();
|
return !(lhs == rhs);
|
||||||
PRAT rRat = r.ToPRAT();
|
}
|
||||||
|
|
||||||
|
bool operator<(Rational const& lhs, Rational const& rhs)
|
||||||
|
{
|
||||||
|
PRAT lhsRat = lhs.ToPRAT();
|
||||||
|
PRAT rhsRat = rhs.ToPRAT();
|
||||||
|
|
||||||
bool result = false;
|
bool result = false;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = rat_le(thisRat, rRat, precision);
|
result = rat_lt(lhsRat, rhsRat, RATIONAL_PRECISION);
|
||||||
}
|
}
|
||||||
catch (DWORD error)
|
catch (DWORD error)
|
||||||
{
|
{
|
||||||
destroyrat(thisRat);
|
destroyrat(lhsRat);
|
||||||
destroyrat(rRat);
|
destroyrat(rhsRat);
|
||||||
throw(error);
|
throw(error);
|
||||||
}
|
}
|
||||||
|
|
||||||
destroyrat(thisRat);
|
destroyrat(lhsRat);
|
||||||
destroyrat(rRat);
|
destroyrat(rhsRat);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rational::IsGreaterEq(Rational const& r, int32_t precision) const
|
bool operator>(Rational const& lhs, Rational const& rhs)
|
||||||
{
|
{
|
||||||
PRAT thisRat = this->ToPRAT();
|
return rhs < lhs;
|
||||||
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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rational::IsEq(Rational const& r, int32_t precision) const
|
bool operator<=(Rational const& lhs, Rational const& rhs)
|
||||||
{
|
{
|
||||||
PRAT thisRat = this->ToPRAT();
|
return !(lhs > rhs);
|
||||||
PRAT rRat = r.ToPRAT();
|
}
|
||||||
|
|
||||||
bool result = false;
|
bool operator>=(Rational const& lhs, Rational const& rhs)
|
||||||
try
|
{
|
||||||
{
|
return !(lhs < rhs);
|
||||||
result = rat_equ(thisRat, rRat, precision);
|
|
||||||
}
|
|
||||||
catch (DWORD error)
|
|
||||||
{
|
|
||||||
destroyrat(thisRat);
|
|
||||||
destroyrat(rRat);
|
|
||||||
throw(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
destroyrat(thisRat);
|
|
||||||
destroyrat(rRat);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
wstring Rational::ToString(uint32_t radix, NUMOBJ_FMT fmt, int32_t precision) const
|
wstring Rational::ToString(uint32_t radix, NUMOBJ_FMT fmt, int32_t precision) const
|
||||||
|
|
|
@ -1002,9 +1002,9 @@ int CCalcEngine::IdcSetAngleTypeDecMode(int idc)
|
||||||
|
|
||||||
bool CCalcEngine::IsCurrentTooBigForTrig()
|
bool CCalcEngine::IsCurrentTooBigForTrig()
|
||||||
{
|
{
|
||||||
if (m_currentVal.IsGreaterEq(m_maxTrigonometricNum, m_precision))
|
if (m_currentVal >= m_maxTrigonometricNum)
|
||||||
{
|
{
|
||||||
m_currentVal = Rational{};
|
m_currentVal = 0;
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -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
|
// Can be converting a dec negative number to Hex/Oct/Bin rep. Use 2's complement form
|
||||||
// Check the range.
|
// Check the range.
|
||||||
if (result.IsLess(0, m_precision))
|
if (result < 0)
|
||||||
{
|
{
|
||||||
// if negative make positive by doing a twos complement
|
// if negative make positive by doing a twos complement
|
||||||
result = -(result) - 1;
|
result = -(result) - 1;
|
||||||
|
@ -83,7 +83,7 @@ void CCalcEngine::DisplayNum(void)
|
||||||
// called.
|
// called.
|
||||||
//
|
//
|
||||||
if (m_bRecord ||
|
if (m_bRecord ||
|
||||||
!gldPrevious.value.IsEq(m_currentVal, m_precision) ||
|
gldPrevious.value != m_currentVal ||
|
||||||
gldPrevious.precision != m_precision ||
|
gldPrevious.precision != m_precision ||
|
||||||
gldPrevious.radix != m_radix ||
|
gldPrevious.radix != m_radix ||
|
||||||
gldPrevious.nFE != (int)m_nFE ||
|
gldPrevious.nFE != (int)m_nFE ||
|
||||||
|
|
|
@ -31,7 +31,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
|
|
||||||
case IDC_RSHF:
|
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;
|
throw CALC_E_NORESULT;
|
||||||
}
|
}
|
||||||
|
@ -55,7 +55,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
}
|
}
|
||||||
|
|
||||||
case IDC_LSHF:
|
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;
|
throw CALC_E_NORESULT;
|
||||||
}
|
}
|
||||||
|
|
|
@ -59,10 +59,13 @@ namespace CalcEngine
|
||||||
Rational Not(Rational const& chopNum, int32_t precision) const;
|
Rational Not(Rational const& chopNum, int32_t precision) const;
|
||||||
|
|
||||||
bool IsZero() const;
|
bool IsZero() const;
|
||||||
bool IsLess(Rational const& r, int32_t precision) const;
|
|
||||||
bool IsLessEq(Rational const& r, int32_t precision) const;
|
friend bool operator==(Rational const& lhs, Rational const& rhs);
|
||||||
bool IsGreaterEq(Rational const& r, int32_t precision) const;
|
friend bool operator!=(Rational const& lhs, Rational const& rhs);
|
||||||
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);
|
||||||
|
|
||||||
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
|
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
|
||||||
uint64_t ToUInt64_t(int32_t precision) const;
|
uint64_t ToUInt64_t(int32_t precision) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue