Convert Rational::Add to + and += operator overrides.

This commit is contained in:
Josh Koon 2019-02-21 10:32:47 -08:00
commit 259d16e1b8
6 changed files with 22 additions and 18 deletions

View file

@ -91,14 +91,14 @@ namespace CalcEngine
return Rational{ Number{ -1 * m_p.Sign(), m_p.Exp(), m_p.Mantissa() }, m_q };
}
Rational Rational::Add(Rational const& rhs, int32_t precision) const
Rational& Rational::operator+=(Rational const& rhs)
{
PRAT lhsRat = this->ToPRAT();
PRAT rhsRat = rhs.ToPRAT();
try
{
addrat(&lhsRat, rhsRat, precision);
addrat(&lhsRat, rhsRat, RATIONAL_PRECISION);
destroyrat(rhsRat);
}
catch (DWORD error)
@ -108,10 +108,16 @@ namespace CalcEngine
throw(error);
}
Rational result = Rational{ lhsRat };
*this = Rational{ lhsRat };
destroyrat(lhsRat);
return result;
return *this;
}
Rational operator+(Rational lhs, Rational const& rhs)
{
lhs += rhs;
return lhs;
}
Rational Rational::Sub(Rational const& rhs, int32_t precision) const

View file

@ -718,7 +718,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
{
/* MPLUS adds m_currentVal to immediate memory and kills the "mem" */
/* indicator if the result is zero. */
Rational result = m_memoryValue->Add(m_currentVal, m_precision);
Rational result = *m_memoryValue + m_currentVal;
m_memoryValue = make_unique<Rational>(TruncateNumForIntMath(result)); // Memory should follow the current int mode
break;
@ -1053,8 +1053,7 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
if ((radix == 10) && fMsb)
{
// If high bit is set, then get the decimal number in negative 2's compl form.
tempRat = tempRat.Not(m_chopNumbers[m_numwidth], m_precision);
tempRat = -(tempRat.Add(1, m_precision));
tempRat = -(tempRat.Not(m_chopNumbers[m_numwidth], m_precision) + 1);
}
result = tempRat.ToString(radix, m_nFE, m_precision);

View file

@ -39,8 +39,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
case IDC_COM:
if (m_radix == 10 && !m_fIntegerMode)
{
result = RationalMath::Integer(rat, m_precision);
result = -(result.Add(1, m_precision));
result = -(RationalMath::Integer(rat, m_precision) + 1);
}
else
{
@ -200,10 +199,10 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
shftRat = Rational{ m_bInv ? 60 : 100 };
secondRat = secondRat.Div(shftRat, m_precision);
minuteRat = minuteRat.Add(secondRat, m_precision);
minuteRat += secondRat;
minuteRat = minuteRat.Div(shftRat, m_precision);
result = degreeRat.Add(minuteRat, m_precision);
result = degreeRat + minuteRat;
}
break;
}

View file

@ -65,7 +65,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
break;
case IDC_ADD:
result = result.Add(rhs, m_precision);
result += rhs;
break;
case IDC_SUB:
@ -90,8 +90,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (fMsb)
{
result = rhs.Not(m_chopNumbers[m_numwidth], m_precision);
result = result.Add(1, m_precision);
result = rhs.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
iNumeratorSign = -1;
}
@ -101,8 +100,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (fMsb)
{
temp = temp.Not(m_chopNumbers[m_numwidth], m_precision);
temp = temp.Add(1, m_precision);
temp = temp.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
iDenominatorSign = -1;
}

View file

@ -26,7 +26,7 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RADIX_TYPE radixtype, NUM_WIDTH numwid
// If high bit is set, then get the decimal number in -ve 2'scompl form.
auto tempResult = m_currentVal.Not(m_chopNumbers[m_numwidth], m_precision);
m_currentVal = -(tempResult.Add(1, m_precision));
m_currentVal = -(tempResult + 1);
}
}

View file

@ -30,12 +30,14 @@ namespace CalcEngine
Number const& Q() const;
Rational operator-() const;
Rational Add(Rational const& rhs, int32_t precision) const;
Rational& operator+=(Rational const& rhs);
Rational Sub(Rational const& rhs, int32_t precision) const;
Rational Mul(Rational const& rhs, int32_t precision) const;
Rational Div(Rational const& rhs, int32_t precision) const;
Rational Mod(Rational const& rhs) const;
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;