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:
Josh Koon 2019-02-21 12:09:13 -08:00
commit 5e4eaf636a
7 changed files with 29 additions and 29 deletions

View file

@ -50,7 +50,7 @@ namespace CalcEngine
destroyrat(pr);
}
Rational::Rational(uint64_t ui, int32_t precision)
Rational::Rational(uint64_t ui)
{
uint32_t hi = HIDWORD(ui);
uint32_t lo = LODWORD(ui);
@ -379,7 +379,7 @@ namespace CalcEngine
return lhs;
}
Rational Rational::Not(Rational const& chopNum, int32_t precision) const
Rational Rational::Not(Rational const& chopNum) const
{
return *this ^ chopNum;
}
@ -475,13 +475,13 @@ namespace CalcEngine
return result;
}
uint64_t Rational::ToUInt64_t(int32_t precision) const
uint64_t Rational::ToUInt64_t() const
{
PRAT rat = this->ToPRAT();
uint64_t result;
try
{
result = rattoUlonglong(rat, RATIONAL_BASE, precision);
result = rattoUlonglong(rat, RATIONAL_BASE, RATIONAL_PRECISION);
}
catch (DWORD error)
{

View file

@ -382,7 +382,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
CheckAndAddLastBinOpToHistory(false);
}
m_lastVal = Rational{};
m_lastVal = 0;
m_bChangeOp = false;
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_lastVal = Rational{};
m_lastVal = 0;
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
// treated as 1 + (3
m_currentVal = Rational{};
m_currentVal = 0;
}
m_nTempCom = 0;
m_nOpCode = 0;
@ -708,7 +708,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
else
{
// Recall immediate memory value.
m_currentVal = Rational{ *m_memoryValue };
m_currentVal = *m_memoryValue;
}
CheckAndAddLastBinOpToHistory();
DisplayNum();
@ -734,7 +734,7 @@ void CCalcEngine::ProcessCommandWorker(WPARAM wParam)
}
case IDC_STORE:
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;
case IDC_PI:
@ -1048,12 +1048,12 @@ wstring CCalcEngine::GetStringForDisplay(Rational const& rat, uint32_t radix)
try
{
uint64_t w64Bits = tempRat.ToUInt64_t(m_precision);
uint64_t w64Bits = tempRat.ToUInt64_t();
bool fMsb = ((w64Bits >> (m_dwWordBitWidth - 1)) & 1);
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) + 1);
tempRat = -(tempRat.Not(m_chopNumbers[m_numwidth]) + 1);
}
result = tempRat.ToString(radix, m_nFE, m_precision);

View file

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

View file

@ -53,12 +53,12 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
{
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;
w64Bits <<= 1; // LShift by 1
w64Bits |= msb; // Set the prev Msb as the current Lsb
result = Rational{ w64Bits, m_precision };
result = w64Bits;
}
break;
@ -68,12 +68,12 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
{
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;
w64Bits >>= 1; //RShift by 1
w64Bits |= (lsb << (m_dwWordBitWidth - 1));
result = Rational{ w64Bits, m_precision };
result = w64Bits;
}
break;
@ -177,7 +177,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
{
if (!m_fIntegerMode)
{
Rational shftRat{ m_bInv ? 100 : 60 };
auto shftRat{ m_bInv ? 100 : 60 };
Rational degreeRat = Integer(rat, m_precision);
@ -193,7 +193,7 @@ CalcEngine::Rational CCalcEngine::SciCalcFunctions(CalcEngine::Rational const& r
// degreeRat == degrees, minuteRat == minutes, secondRat == seconds
//
shftRat = Rational{ m_bInv ? 60 : 100 };
shftRat = m_bInv ? 60 : 100;
secondRat /= shftRat;
minuteRat = (minuteRat + secondRat) / shftRat;

View file

@ -11,7 +11,7 @@ using namespace CalcEngine::RationalMath;
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.
auto result = (!lhs.IsZero() ? lhs : Rational{});
auto result = (!lhs.IsZero() ? lhs : 0);
try
{
@ -36,7 +36,7 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
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;
Rational holdVal = result;
@ -84,22 +84,22 @@ CalcEngine::Rational CCalcEngine::DoOperation(int operation, CalcEngine::Rationa
if (m_fIntegerMode)
{
uint64_t w64Bits = rhs.ToUInt64_t(m_precision);
uint64_t w64Bits = rhs.ToUInt64_t();
bool fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
if (fMsb)
{
result = rhs.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
result = rhs.Not(m_chopNumbers[m_numwidth]) + 1;
iNumeratorSign = -1;
}
w64Bits = temp.ToUInt64_t(m_precision);
w64Bits = temp.ToUInt64_t();
fMsb = (w64Bits >> (m_dwWordBitWidth - 1)) & 1;
if (fMsb)
{
temp = temp.Not(m_chopNumbers[m_numwidth], m_precision) + 1;
temp = temp.Not(m_chopNumbers[m_numwidth]) + 1;
iDenominatorSign = -1;
}

View file

@ -18,13 +18,13 @@ void CCalcEngine::SetRadixTypeAndNumWidth(RADIX_TYPE radixtype, NUM_WIDTH numwid
// back to 1111,1111,1000,0001 when in Word mode.
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
if (fMsb)
{
// 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);
}

View file

@ -21,7 +21,7 @@ namespace CalcEngine
Rational(Number const& p, Number const& q) noexcept;
Rational(int32_t i);
Rational(uint32_t ui);
Rational(uint64_t ui, int32_t precision);
Rational(uint64_t ui);
explicit Rational(PRAT prat) noexcept;
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);
Rational Not(Rational const& chopNum, int32_t precision) const;
Rational Not(Rational const& chopNum) const;
bool IsZero() const;
@ -68,7 +68,7 @@ namespace CalcEngine
friend bool operator>=(Rational const& lhs, Rational const& rhs);
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:
Number m_p;