mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 06:13:14 -07:00
Convert Rational::Negate to an operator override
This commit is contained in:
parent
9c6ef250d0
commit
08e973a0b6
7 changed files with 12 additions and 12 deletions
|
@ -86,9 +86,9 @@ namespace CalcEngine
|
||||||
return m_q;
|
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};
|
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::Add(Rational const& rhs, int32_t precision) const
|
||||||
|
|
|
@ -691,7 +691,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
|
||||||
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
|
m_HistoryCollector.AddOpndToHistory(m_numberString, m_currentVal);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_currentVal = m_currentVal.Negate();
|
m_currentVal = -(m_currentVal);
|
||||||
|
|
||||||
DisplayNum();
|
DisplayNum();
|
||||||
m_HistoryCollector.AddUnaryOpToHistory(IDC_SIGN, m_bInv, m_angletype);
|
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.
|
// 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.Not(m_chopNumbers[m_numwidth], m_precision);
|
||||||
tempRat = tempRat.Add(1, m_precision);
|
tempRat = -(tempRat.Add(1, m_precision));
|
||||||
tempRat = tempRat.Negate();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
result = tempRat.ToString(radix, m_nFE, m_precision);
|
result = tempRat.ToString(radix, m_nFE, m_precision);
|
||||||
|
|
|
@ -64,7 +64,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
|
||||||
if (result.IsLess(0, m_precision))
|
if (result.IsLess(0, m_precision))
|
||||||
{
|
{
|
||||||
// if negative make positive by doing a twos complement
|
// if negative make positive by doing a twos complement
|
||||||
result = result.Negate();
|
result = -(result);
|
||||||
result = result.Sub(1, m_precision);
|
result = result.Sub(1, m_precision);
|
||||||
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
|
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,8 +40,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
if (m_radix == 10 && !m_fIntegerMode)
|
if (m_radix == 10 && !m_fIntegerMode)
|
||||||
{
|
{
|
||||||
result = RationalMath::Integer(rat, m_precision);
|
result = RationalMath::Integer(rat, m_precision);
|
||||||
result = result.Add(1, m_precision);
|
result = -(result.Add(1, m_precision));
|
||||||
result = result.Negate();
|
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
|
|
|
@ -121,7 +121,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
|
|
||||||
if (m_fIntegerMode && iFinalSign == -1)
|
if (m_fIntegerMode && iFinalSign == -1)
|
||||||
{
|
{
|
||||||
result = Integer(result, m_precision).Negate();
|
result = -(Integer(result, m_precision));
|
||||||
}
|
}
|
||||||
|
|
||||||
break;
|
break;
|
||||||
|
|
|
@ -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.
|
// 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);
|
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));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -10,6 +10,9 @@ namespace CalcEngine
|
||||||
// RatPack calculations currently support up to Base64.
|
// RatPack calculations currently support up to Base64.
|
||||||
inline constexpr uint32_t RATIONAL_BASE = 10;
|
inline constexpr uint32_t RATIONAL_BASE = 10;
|
||||||
|
|
||||||
|
// Default Precision to use for Rational calculations
|
||||||
|
static constexpr int32_t RATIONAL_PRECISION = 128;
|
||||||
|
|
||||||
class Rational
|
class Rational
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
@ -26,7 +29,7 @@ namespace CalcEngine
|
||||||
Number const& P() const;
|
Number const& P() const;
|
||||||
Number const& Q() const;
|
Number const& Q() const;
|
||||||
|
|
||||||
Rational Negate() const;
|
Rational operator-() const;
|
||||||
Rational Add(Rational const& rhs, int32_t precision) const;
|
Rational Add(Rational const& rhs, int32_t precision) const;
|
||||||
Rational Sub(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;
|
Rational Mul(Rational const& rhs, int32_t precision) const;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue