mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-21 22:03:11 -07:00
Convert Rational::Rsh and ::Lsh to use >>=, >>, <<=, << operator overrides
This commit is contained in:
parent
34b27977f6
commit
5f641c1389
3 changed files with 61 additions and 46 deletions
|
@ -55,7 +55,7 @@ namespace CalcEngine
|
|||
uint32_t hi = HIDWORD(ui);
|
||||
uint32_t lo = LODWORD(ui);
|
||||
|
||||
Rational temp = Rational{ hi }.Lsh(32, precision).Or(lo, precision);
|
||||
Rational temp = (Rational{ hi } << 32).Or(lo, precision);
|
||||
|
||||
m_p = Number{ temp.P() };
|
||||
m_q = Number{ temp.Q() };
|
||||
|
@ -206,6 +206,52 @@ namespace CalcEngine
|
|||
return *this;
|
||||
}
|
||||
|
||||
Rational& Rational::operator<<=(Rational const& rhs)
|
||||
{
|
||||
PRAT lhsRat = this->ToPRAT();
|
||||
PRAT rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
lshrat(&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
|
||||
{
|
||||
rshrat(&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)
|
||||
{
|
||||
lhs += rhs;
|
||||
|
@ -236,50 +282,16 @@ namespace CalcEngine
|
|||
return lhs;
|
||||
}
|
||||
|
||||
Rational Rational::Lsh(Rational const& rhs, int32_t precision) const
|
||||
Rational operator<<(Rational lhs, Rational const& rhs)
|
||||
{
|
||||
PRAT lhsRat = this->ToPRAT();
|
||||
PRAT rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
lshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
destroyrat(lhsRat);
|
||||
destroyrat(rhsRat);
|
||||
throw(error);
|
||||
}
|
||||
|
||||
Rational result = Rational{ lhsRat };
|
||||
destroyrat(lhsRat);
|
||||
|
||||
return result;
|
||||
lhs <<= rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
Rational Rational::Rsh(Rational const& rhs, int32_t precision) const
|
||||
Rational operator>>(Rational lhs, Rational const& rhs)
|
||||
{
|
||||
PRAT lhsRat = this->ToPRAT();
|
||||
PRAT rhsRat = rhs.ToPRAT();
|
||||
|
||||
try
|
||||
{
|
||||
rshrat(&lhsRat, rhsRat, RATIONAL_BASE, precision);
|
||||
destroyrat(rhsRat);
|
||||
}
|
||||
catch (DWORD error)
|
||||
{
|
||||
destroyrat(lhsRat);
|
||||
destroyrat(rhsRat);
|
||||
throw(error);
|
||||
}
|
||||
|
||||
Rational result = Rational{ lhsRat };
|
||||
destroyrat(lhsRat);
|
||||
|
||||
return result;
|
||||
lhs >>= rhs;
|
||||
return lhs;
|
||||
}
|
||||
|
||||
Rational Rational::Not(Rational const& chopNum, int32_t precision) const
|
||||
|
|
|
@ -40,13 +40,13 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
|||
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||
|
||||
Rational holdVal = result;
|
||||
result = rhs.Rsh(holdVal, m_precision);
|
||||
result = rhs >> holdVal;
|
||||
|
||||
if (fMsb)
|
||||
{
|
||||
result = Integer(result, m_precision);
|
||||
|
||||
auto tempRat = m_chopNumbers[m_numwidth].Rsh(holdVal, m_precision);
|
||||
auto tempRat = m_chopNumbers[m_numwidth] >> holdVal;
|
||||
tempRat = Integer(tempRat, m_precision);
|
||||
|
||||
tempRat = tempRat.Xor(m_chopNumbers[m_numwidth], m_precision);
|
||||
|
@ -61,7 +61,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
|||
throw CALC_E_NORESULT;
|
||||
}
|
||||
|
||||
result = rhs.Lsh(result, m_precision);
|
||||
result = rhs << result;
|
||||
break;
|
||||
|
||||
case IDC_ADD:
|
||||
|
|
|
@ -36,14 +36,17 @@ namespace CalcEngine
|
|||
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);
|
||||
|
||||
Rational Lsh(Rational const& r, int32_t precision) const;
|
||||
Rational Rsh(Rational const& r, int32_t precision) const;
|
||||
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 And(Rational const& r, int32_t precision) const;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue