mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-22 14:13:30 -07:00
Remove unnecessary precision arguments from Rational class and remove use of explicit Rational constructors in favor of implicit conversions for value types
This commit is contained in:
parent
0d298e13f3
commit
5e4eaf636a
7 changed files with 29 additions and 29 deletions
|
@ -50,7 +50,7 @@ namespace CalcEngine
|
||||||
destroyrat(pr);
|
destroyrat(pr);
|
||||||
}
|
}
|
||||||
|
|
||||||
Rational::Rational(uint64_t ui, int32_t precision)
|
Rational::Rational(uint64_t ui)
|
||||||
{
|
{
|
||||||
uint32_t hi = HIDWORD(ui);
|
uint32_t hi = HIDWORD(ui);
|
||||||
uint32_t lo = LODWORD(ui);
|
uint32_t lo = LODWORD(ui);
|
||||||
|
@ -379,7 +379,7 @@ namespace CalcEngine
|
||||||
return lhs;
|
return lhs;
|
||||||
}
|
}
|
||||||
|
|
||||||
Rational Rational::Not(Rational const& chopNum, int32_t precision) const
|
Rational Rational::Not(Rational const& chopNum) const
|
||||||
{
|
{
|
||||||
return *this ^ chopNum;
|
return *this ^ chopNum;
|
||||||
}
|
}
|
||||||
|
@ -475,13 +475,13 @@ namespace CalcEngine
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t Rational::ToUInt64_t(int32_t precision) const
|
uint64_t Rational::ToUInt64_t() const
|
||||||
{
|
{
|
||||||
PRAT rat = this->ToPRAT();
|
PRAT rat = this->ToPRAT();
|
||||||
uint64_t result;
|
uint64_t result;
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
result = rattoUlonglong(rat, RATIONAL_BASE, precision);
|
result = rattoUlonglong(rat, RATIONAL_BASE, RATIONAL_PRECISION);
|
||||||
}
|
}
|
||||||
catch (DWORD error)
|
catch (DWORD error)
|
||||||
{
|
{
|
||||||
|
|
|
@ -382,7 +382,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
|
||||||
CheckAndAddLastBinOpToHistory(false);
|
CheckAndAddLastBinOpToHistory(false);
|
||||||
}
|
}
|
||||||
|
|
||||||
m_lastVal = Rational{};
|
m_lastVal = 0;
|
||||||
|
|
||||||
m_bChangeOp = false;
|
m_bChangeOp = false;
|
||||||
m_precedenceOpCount = m_nTempCom = m_nLastCom = m_nOpCode = m_openParenCount = 0;
|
m_precedenceOpCount = m_nTempCom = m_nLastCom = m_nOpCode = m_openParenCount = 0;
|
||||||
|
@ -563,12 +563,12 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
|
||||||
m_nPrecOp[m_precedenceOpCount++] = 0;
|
m_nPrecOp[m_precedenceOpCount++] = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
m_lastVal = Rational{};
|
m_lastVal = 0;
|
||||||
if (IsBinOpCode(m_nLastCom))
|
if (IsBinOpCode(m_nLastCom))
|
||||||
{
|
{
|
||||||
// We want 1 + ( to start as 1 + (0. Any number you type replaces 0. But if it is 1 + 3 (, it is
|
// We want 1 + ( to start as 1 + (0. Any number you type replaces 0. But if it is 1 + 3 (, it is
|
||||||
// treated as 1 + (3
|
// treated as 1 + (3
|
||||||
m_currentVal = Rational{};
|
m_currentVal = 0;
|
||||||
}
|
}
|
||||||
m_nTempCom = 0;
|
m_nTempCom = 0;
|
||||||
m_nOpCode = 0;
|
m_nOpCode = 0;
|
||||||
|
@ -708,7 +708,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
// Recall immediate memory value.
|
// Recall immediate memory value.
|
||||||
m_currentVal = Rational{ *m_memoryValue };
|
m_currentVal = *m_memoryValue;
|
||||||
}
|
}
|
||||||
CheckAndAddLastBinOpToHistory();
|
CheckAndAddLastBinOpToHistory();
|
||||||
DisplayNum();
|
DisplayNum();
|
||||||
|
@ -734,7 +734,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
|
||||||
}
|
}
|
||||||
case IDC_STORE:
|
case IDC_STORE:
|
||||||
case IDC_MCLEAR:
|
case IDC_MCLEAR:
|
||||||
m_memoryValue = make_unique<Rational>(wParam == IDC_STORE ? TruncateNumForIntMath(m_currentVal) : Rational{});
|
m_memoryValue = make_unique<Rational>(wParam == IDC_STORE ? TruncateNumForIntMath(m_currentVal) : 0);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case IDC_PI:
|
case IDC_PI:
|
||||||
|
@ -1048,12 +1048,12 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
uint64_t w64Bits = tempRat.ToUInt64_t(m_precision);
|
uint64_t w64Bits = tempRat.ToUInt64_t();
|
||||||
bool fMsb = ((w64Bits >> (m_dwWordBitWidth - 1)) & 1);
|
bool fMsb = ((w64Bits >> (m_dwWordBitWidth - 1)) & 1);
|
||||||
if ((radix == 10) && fMsb)
|
if ((radix == 10) && fMsb)
|
||||||
{
|
{
|
||||||
// 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) + 1);
|
tempRat = -(tempRat.Not(m_chopNumbers[m_numwidth]) + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
result = tempRat.ToString(radix, m_nFE, m_precision);
|
result = tempRat.ToString(radix, m_nFE, m_precision);
|
||||||
|
|
|
@ -65,7 +65,7 @@ CalcEngine::Rational CCalcEngine::TruncateNumForIntMath(CalcEngine::Rational con
|
||||||
{
|
{
|
||||||
// if negative make positive by doing a twos complement
|
// if negative make positive by doing a twos complement
|
||||||
result = -(result) - 1;
|
result = -(result) - 1;
|
||||||
result = result.Not(m_chopNumbers[m_numwidth], m_precision);
|
result = result.Not(m_chopNumbers[m_numwidth]);
|
||||||
}
|
}
|
||||||
|
|
||||||
result &= m_chopNumbers[m_numwidth];
|
result &= m_chopNumbers[m_numwidth];
|
||||||
|
|
|
@ -53,12 +53,12 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
{
|
{
|
||||||
result = Integer(rat, m_precision);
|
result = Integer(rat, m_precision);
|
||||||
|
|
||||||
uint64_t w64Bits = result.ToUInt64_t(m_precision);
|
uint64_t w64Bits = result.ToUInt64_t();
|
||||||
uint64_t msb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
uint64_t msb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||||
w64Bits <<= 1; // LShift by 1
|
w64Bits <<= 1; // LShift by 1
|
||||||
w64Bits |= msb; // Set the prev Msb as the current Lsb
|
w64Bits |= msb; // Set the prev Msb as the current Lsb
|
||||||
|
|
||||||
result = Rational{ w64Bits, m_precision };
|
result = w64Bits;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -68,12 +68,12 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
{
|
{
|
||||||
result = Integer(rat, m_precision);
|
result = Integer(rat, m_precision);
|
||||||
|
|
||||||
uint64_t w64Bits = result.ToUInt64_t(m_precision);
|
uint64_t w64Bits = result.ToUInt64_t();
|
||||||
uint64_t lsb = ((w64Bits & 0x01) == 1) ? 1 : 0;
|
uint64_t lsb = ((w64Bits & 0x01) == 1) ? 1 : 0;
|
||||||
w64Bits >>= 1; //RShift by 1
|
w64Bits >>= 1; //RShift by 1
|
||||||
w64Bits |= (lsb << (m_dwWordBitWidth - 1));
|
w64Bits |= (lsb << (m_dwWordBitWidth - 1));
|
||||||
|
|
||||||
result = Rational{ w64Bits, m_precision };
|
result = w64Bits;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
@ -177,7 +177,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
{
|
{
|
||||||
if (!m_fIntegerMode)
|
if (!m_fIntegerMode)
|
||||||
{
|
{
|
||||||
Rational shftRat{ m_bInv ? 100 : 60 };
|
auto shftRat{ m_bInv ? 100 : 60 };
|
||||||
|
|
||||||
Rational degreeRat = Integer(rat, m_precision);
|
Rational degreeRat = Integer(rat, m_precision);
|
||||||
|
|
||||||
|
@ -193,7 +193,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
|
||||||
// degreeRat == degrees, minuteRat == minutes, secondRat == seconds
|
// degreeRat == degrees, minuteRat == minutes, secondRat == seconds
|
||||||
//
|
//
|
||||||
|
|
||||||
shftRat = Rational{ m_bInv ? 60 : 100 };
|
shftRat = m_bInv ? 60 : 100;
|
||||||
secondRat /= shftRat;
|
secondRat /= shftRat;
|
||||||
|
|
||||||
minuteRat = (minuteRat + secondRat) / shftRat;
|
minuteRat = (minuteRat + secondRat) / shftRat;
|
||||||
|
|
|
@ -11,7 +11,7 @@ using namespace CalcEngine::RationalMath;
|
||||||
CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rational const& lhs, CalcEngine::Rational const& rhs)
|
CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rational const& lhs, CalcEngine::Rational const& rhs)
|
||||||
{
|
{
|
||||||
// Remove any variance in how 0 could be represented in rat e.g. -0, 0/n, etc.
|
// Remove any variance in how 0 could be represented in rat e.g. -0, 0/n, etc.
|
||||||
auto result = (!lhs.IsZero() ? lhs : Rational{});
|
auto result = (!lhs.IsZero() ? lhs : 0);
|
||||||
|
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -36,7 +36,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
throw CALC_E_NORESULT;
|
throw CALC_E_NORESULT;
|
||||||
}
|
}
|
||||||
|
|
||||||
uint64_t w64Bits = rhs.ToUInt64_t(m_precision);
|
uint64_t w64Bits = rhs.ToUInt64_t();
|
||||||
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||||
|
|
||||||
Rational holdVal = result;
|
Rational holdVal = result;
|
||||||
|
@ -84,22 +84,22 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
|
||||||
|
|
||||||
if (m_fIntegerMode)
|
if (m_fIntegerMode)
|
||||||
{
|
{
|
||||||
uint64_t w64Bits = rhs.ToUInt64_t(m_precision);
|
uint64_t w64Bits = rhs.ToUInt64_t();
|
||||||
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||||
|
|
||||||
if (fMsb)
|
if (fMsb)
|
||||||
{
|
{
|
||||||
result = rhs.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
|
result = rhs.Not(m_chopNumbers[m_numwidth]) + 1;
|
||||||
|
|
||||||
iNumeratorSign = -1;
|
iNumeratorSign = -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
w64Bits = temp.ToUInt64_t(m_precision);
|
w64Bits = temp.ToUInt64_t();
|
||||||
fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
|
||||||
|
|
||||||
if (fMsb)
|
if (fMsb)
|
||||||
{
|
{
|
||||||
temp = temp.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
|
temp = temp.Not(m_chopNumbers[m_numwidth]) + 1;
|
||||||
|
|
||||||
iDenominatorSign = -1;
|
iDenominatorSign = -1;
|
||||||
}
|
}
|
||||||
|
|
|
@ -18,13 +18,13 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RADIX_TYPE radixtype, NUM_WIDTH numwid
|
||||||
// back to 1111,1111,1000,0001 when in Word mode.
|
// back to 1111,1111,1000,0001 when in Word mode.
|
||||||
if (m_fIntegerMode)
|
if (m_fIntegerMode)
|
||||||
{
|
{
|
||||||
uint64_t w64Bits = m_currentVal.ToUInt64_t(m_precision);
|
uint64_t w64Bits = m_currentVal.ToUInt64_t();
|
||||||
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; // make sure you use the old width
|
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1; // make sure you use the old width
|
||||||
|
|
||||||
if (fMsb)
|
if (fMsb)
|
||||||
{
|
{
|
||||||
// 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_currentVal = -(tempResult + 1);
|
m_currentVal = -(tempResult + 1);
|
||||||
}
|
}
|
||||||
|
|
|
@ -21,7 +21,7 @@ namespace CalcEngine
|
||||||
Rational(Number const& p, Number const& q) noexcept;
|
Rational(Number const& p, Number const& q) noexcept;
|
||||||
Rational(int32_t i);
|
Rational(int32_t i);
|
||||||
Rational(uint32_t ui);
|
Rational(uint32_t ui);
|
||||||
Rational(uint64_t ui, int32_t precision);
|
Rational(uint64_t ui);
|
||||||
|
|
||||||
explicit Rational(PRAT prat) noexcept;
|
explicit Rational(PRAT prat) noexcept;
|
||||||
PRAT ToPRAT() const;
|
PRAT ToPRAT() const;
|
||||||
|
@ -56,7 +56,7 @@ 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);
|
||||||
|
|
||||||
Rational Not(Rational const& chopNum, int32_t precision) const;
|
Rational Not(Rational const& chopNum) const;
|
||||||
|
|
||||||
bool IsZero() const;
|
bool IsZero() const;
|
||||||
|
|
||||||
|
@ -68,7 +68,7 @@ namespace CalcEngine
|
||||||
friend bool operator>=(Rational const& lhs, Rational const& rhs);
|
friend bool operator>=(Rational const& lhs, Rational const& rhs);
|
||||||
|
|
||||||
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
|
std::wstring ToString(uint32_t radix, NUMOBJ_FMT format, int32_t precision) const;
|
||||||
uint64_t ToUInt64_t(int32_t precision) const;
|
uint64_t ToUInt64_t() const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Number m_p;
|
Number m_p;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue