mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 06:13:14 -07:00
Convert Rational::And, ::Or, ::Xor to use &=, &, |=, |, ^=, ^ operator overrides
This commit is contained in:
parent
5f641c1389
commit
2fecd35fd2
6 changed files with 103 additions and 81 deletions
|
@ -55,7 +55,7 @@ namespace CalcEngine
|
||||||
uint32_t hi = HIDWORD(ui);
|
uint32_t hi = HIDWORD(ui);
|
||||||
uint32_t lo = LODWORD(ui);
|
uint32_t lo = LODWORD(ui);
|
||||||
|
|
||||||
Rational temp = (Rational{ hi } << 32).Or(lo, precision);
|
Rational temp = (Rational{ hi } << 32) | lo;
|
||||||
|
|
||||||
m_p = Number{ temp.P() };
|
m_p = Number{ temp.P() };
|
||||||
m_q = Number{ temp.Q() };
|
m_q = Number{ temp.Q() };
|
||||||
|
@ -252,6 +252,73 @@ namespace CalcEngine
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rational& Rational::operator&=(Rational const& rhs)
|
||||||
|
{
|
||||||
|
PRAT lhsRat = this->ToPRAT();
|
||||||
|
PRAT rhsRat = rhs.ToPRAT();
|
||||||
|
|
||||||
|
try
|
||||||
|
{
|
||||||
|
andrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
}
|
||||||
|
catch (DWORD error)
|
||||||
|
{
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
throw(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
*this = Rational{ lhsRat };
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rational& Rational::operator|=(Rational const& rhs)
|
||||||
|
{
|
||||||
|
PRAT lhsRat = this->ToPRAT();
|
||||||
|
PRAT rhsRat = rhs.ToPRAT();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
orrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
}
|
||||||
|
catch (DWORD error)
|
||||||
|
{
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
throw(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
*this = Rational{ lhsRat };
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rational& Rational::operator^=(Rational const& rhs)
|
||||||
|
{
|
||||||
|
PRAT lhsRat = this->ToPRAT();
|
||||||
|
PRAT rhsRat = rhs.ToPRAT();
|
||||||
|
try
|
||||||
|
{
|
||||||
|
xorrat(&lhsRat, rhsRat, RATIONAL_BASE, RATIONAL_PRECISION);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
}
|
||||||
|
catch (DWORD error)
|
||||||
|
{
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
destroyrat(rhsRat);
|
||||||
|
throw(error);
|
||||||
|
}
|
||||||
|
|
||||||
|
*this = Rational{ lhsRat };
|
||||||
|
destroyrat(lhsRat);
|
||||||
|
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
Rational operator+(Rational lhs, Rational const& rhs)
|
Rational operator+(Rational lhs, Rational const& rhs)
|
||||||
{
|
{
|
||||||
lhs += rhs;
|
lhs += rhs;
|
||||||
|
@ -294,76 +361,27 @@ namespace CalcEngine
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Rational operator&(Rational lhs, Rational const& rhs)
|
||||||
|
{
|
||||||
|
lhs &= rhs;
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rational operator|(Rational lhs, Rational const& rhs)
|
||||||
|
{
|
||||||
|
lhs |= rhs;
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
|
||||||
|
Rational operator^(Rational lhs, Rational const& rhs)
|
||||||
|
{
|
||||||
|
lhs ^= rhs;
|
||||||
|
return lhs;
|
||||||
|
}
|
||||||
|
|
||||||
Rational Rational::Not(Rational const& chopNum, int32_t precision) const
|
Rational Rational::Not(Rational const& chopNum, int32_t precision) const
|
||||||
{
|
{
|
||||||
return this->Xor(chopNum, precision);
|
return *this ^ chopNum;
|
||||||
}
|
|
||||||
|
|
||||||
Rational Rational::And(Rational const& rhs, int32_t precision) const
|
|
||||||
{
|
|
||||||
PRAT lhsRat = this->ToPRAT();
|
|
||||||
PRAT rhsRat = rhs.ToPRAT();
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
andrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
}
|
|
||||||
catch (DWORD error)
|
|
||||||
{
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
throw(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Rational result = Rational{ lhsRat };
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Rational Rational::Or(Rational const& rhs, int32_t precision) const
|
|
||||||
{
|
|
||||||
PRAT lhsRat = this->ToPRAT();
|
|
||||||
PRAT rhsRat = rhs.ToPRAT();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
orrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
}
|
|
||||||
catch (DWORD error)
|
|
||||||
{
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
throw(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Rational result = Rational{ lhsRat };
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
Rational Rational::Xor(Rational const& rhs, int32_t precision) const
|
|
||||||
{
|
|
||||||
PRAT lhsRat = this->ToPRAT();
|
|
||||||
PRAT rhsRat = rhs.ToPRAT();
|
|
||||||
try
|
|
||||||
{
|
|
||||||
xorrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
}
|
|
||||||
catch (DWORD error)
|
|
||||||
{
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
destroyrat(rhsRat);
|
|
||||||
throw(error);
|
|
||||||
}
|
|
||||||
|
|
||||||
Rational result = Rational{ lhsRat };
|
|
||||||
destroyrat(lhsRat);
|
|
||||||
|
|
||||||
return result;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Rational::IsZero() const
|
bool Rational::IsZero() const
|
||||||
|
|
|
@ -68,7 +68,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
|
||||||
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
|
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = result.And(m_chopNumbers[m_numwidth], m_precision);
|
result &= m_chopNumbers[m_numwidth];
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
|
@ -43,7 +43,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
result = rat.Xor(m_chopNumbers[m_numwidth], m_precision);
|
result = rat ^ m_chopNumbers[m_numwidth];
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
|
|
@ -18,15 +18,15 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
switch (operation)
|
switch (operation)
|
||||||
{
|
{
|
||||||
case IDC_AND:
|
case IDC_AND:
|
||||||
result = result.And(rhs, m_precision);
|
result &= rhs;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IDC_OR:
|
case IDC_OR:
|
||||||
result = result.Or(rhs, m_precision);
|
result |= rhs;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IDC_XOR:
|
case IDC_XOR:
|
||||||
result = result.Xor(rhs, m_precision);
|
result ^= rhs;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IDC_RSHF:
|
case IDC_RSHF:
|
||||||
|
@ -49,8 +49,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
auto tempRat = m_chopNumbers[m_numwidth] >> holdVal;
|
auto tempRat = m_chopNumbers[m_numwidth] >> holdVal;
|
||||||
tempRat = Integer(tempRat, m_precision);
|
tempRat = Integer(tempRat, m_precision);
|
||||||
|
|
||||||
tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_precision);
|
result |= tempRat ^ m_chopNumbers[m_numwidth];
|
||||||
result = result.Or(tempRat, m_precision);
|
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
|
@ -89,11 +89,11 @@ bool CCalcEngine::TryToggleBit(CalcEngine::Rational& rat, DWORD wbitno)
|
||||||
{
|
{
|
||||||
// This is the same work around happenning in SciCalcFunctions. Ought to move to intrat function itself.
|
// This is the same work around happenning in SciCalcFunctions. Ought to move to intrat function itself.
|
||||||
// Basic bug is there which doesn't treat 0/ n as 0, or -0 as 0 etc.
|
// Basic bug is there which doesn't treat 0/ n as 0, or -0 as 0 etc.
|
||||||
result = Rational{};
|
result = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
auto pow = Pow(2, static_cast<int32_t>(wbitno), m_precision);
|
auto pow = Pow(2, static_cast<int32_t>(wbitno), m_precision);
|
||||||
rat = result.Xor(pow, m_precision);
|
rat = result ^ pow;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
|
@ -39,6 +39,10 @@ namespace CalcEngine
|
||||||
Rational& operator<<=(Rational const& rhs);
|
Rational& operator<<=(Rational const& rhs);
|
||||||
Rational& operator>>=(Rational const& rhs);
|
Rational& operator>>=(Rational const& rhs);
|
||||||
|
|
||||||
|
Rational& operator&=(Rational const& rhs);
|
||||||
|
Rational& operator|=(Rational const& rhs);
|
||||||
|
Rational& operator^=(Rational const& rhs);
|
||||||
|
|
||||||
friend Rational operator+(Rational lhs, Rational const& rhs);
|
friend Rational operator+(Rational lhs, Rational const& rhs);
|
||||||
friend Rational operator-(Rational lhs, Rational const& rhs);
|
friend Rational operator-(Rational lhs, Rational const& rhs);
|
||||||
friend Rational operator*(Rational lhs, Rational const& rhs);
|
friend Rational operator*(Rational lhs, Rational const& rhs);
|
||||||
|
@ -48,10 +52,11 @@ namespace CalcEngine
|
||||||
friend Rational operator<<(Rational lhs, Rational const& rhs);
|
friend Rational operator<<(Rational lhs, Rational const& rhs);
|
||||||
friend Rational operator>>(Rational lhs, Rational const& rhs);
|
friend Rational operator>>(Rational lhs, Rational const& rhs);
|
||||||
|
|
||||||
|
friend Rational operator&(Rational lhs, Rational const& rhs);
|
||||||
|
friend Rational operator|(Rational lhs, Rational const& rhs);
|
||||||
|
friend Rational operator^(Rational lhs, Rational const& rhs);
|
||||||
|
|
||||||
Rational Not(Rational const& chopNum, int32_t precision) const;
|
Rational Not(Rational const& chopNum, int32_t precision) const;
|
||||||
Rational And(Rational const& r, int32_t precision) const;
|
|
||||||
Rational Or(Rational const& r, int32_t precision) const;
|
|
||||||
Rational Xor(Rational const& r, int32_t precision) const;
|
|
||||||
|
|
||||||
bool IsZero() const;
|
bool IsZero() const;
|
||||||
bool IsLess(Rational const& r, int32_t precision) const;
|
bool IsLess(Rational const& r, int32_t precision) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue