Convert Rational::Negate to an operator override

This commit is contained in:
Josh Koon 2019-02-21 10:18:50 -08:00
commit 08e973a0b6
7 changed files with 12 additions and 12 deletions

View file

@ -86,7 +86,7 @@ namespace CalcEngine
return m_q;
}
Rational Rational::Negate() const
Rational Rational::operator-() const
{
return Rational{ Number{ -1 * m_p.Sign(), m_p.Exp(), m_p.Mantissa() }, m_q };
}

View file

@ -691,7 +691,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
}
m_currentVal = m_currentVal.Negate();
m_currentVal = -(m_currentVal);
DisplayNum();
m_HistoryCollector.AddUnaryOpToHistory(IDC_SIGN, m_bInv, m_angletype);
@ -1054,8 +1054,7 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
{
// 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.Negate();
tempRat = -(tempRat.Add(1, m_precision));
}
result = tempRat.ToString(radix, m_nFE, m_precision);

View file

@ -64,7 +64,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
if (result.IsLess(0, m_precision))
{
// if negative make positive by doing a twos complement
result = result.Negate();
result = -(result);
result = result.Sub(1, m_precision);
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
}

View file

@ -40,8 +40,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
if (m_radix == 10 && !m_fIntegerMode)
{
result = RationalMath::Integer(rat, m_precision);
result = result.Add(1, m_precision);
result = result.Negate();
result = -(result.Add(1, m_precision));
}
else
{

View file

@ -121,7 +121,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (m_fIntegerMode && iFinalSign == -1)
{
result = Integer(result, m_precision).Negate();
result = -(Integer(result, m_precision));
}
break;

View file

@ -25,9 +25,8 @@ 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);
tempResult = tempResult.Add(1, m_precision);
m_currentVal = tempResult.Negate();
m_currentVal = -(tempResult.Add(1, m_precision));
}
}

View file

@ -10,6 +10,9 @@ namespace CalcEngine
// RatPack calculations currently support up to Base64.
inline constexpr uint32_t RATIONAL_BASE = 10;
// Default Precision to use for Rational calculations
static constexpr int32_t RATIONAL_PRECISION = 128;
class Rational
{
public:
@ -26,7 +29,7 @@ namespace CalcEngine
Number const& P() const;
Number const& Q() const;
Rational Negate() const;
Rational operator-() const;
Rational Add(Rational const& rhs, int32_t precision) const;
Rational Sub(Rational const& rhs, int32_t precision) const;
Rational Mul(Rational const& rhs, int32_t precision) const;