diff --git a/src/CalcManager/CEngine/scicomm.cpp b/src/CalcManager/CEngine/scicomm.cpp index f7509ab..b738671 100644 --- a/src/CalcManager/CEngine/scicomm.cpp +++ b/src/CalcManager/CEngine/scicomm.cpp @@ -36,18 +36,15 @@ namespace IDC_RSHF,3, IDC_LSHF,3, IDC_RSHFL,3, IDC_MOD,3, IDC_DIV,3, IDC_MUL,3, IDC_PWR,4, IDC_ROOT,4, IDC_LOGBASEX,4 }; - unsigned int iPrec; - iPrec = 0; - while ((iPrec < size(rgbPrec)) && (nopCode != rgbPrec[iPrec])) + for (unsigned int iPrec = 0; iPrec < size(rgbPrec); iPrec += 2) { - iPrec += 2; + if (nopCode == rgbPrec[iPrec]) + { + return rgbPrec[iPrec + 1]; + } } - if (iPrec >= size(rgbPrec)) - { - iPrec = 0; - } - return rgbPrec[iPrec + 1]; + return 0; } } @@ -104,8 +101,6 @@ void CCalcEngine::ProcessCommand(OpCode wParam) void CCalcEngine::ProcessCommandWorker(OpCode wParam) { - int nx, ni; - // Save the last command. Some commands are not saved in this manor, these // commands are: // Inv, Deg, Rad, Grad, Stat, FE, MClear, Back, and Exp. The excluded @@ -163,14 +158,11 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) DisplayNum(); // Causes 3.000 to shrink to 3. on first op. } } - else + else if (IsDigitOpCode(wParam) || wParam == IDC_PNT) { - if (IsDigitOpCode(wParam) || wParam == IDC_PNT) - { - m_bRecord = true; - m_input.Clear(); - CheckAndAddLastBinOpToHistory(); - } + m_bRecord = true; + m_input.Clear(); + CheckAndAddLastBinOpToHistory(); } // Interpret digit keys. @@ -203,7 +195,6 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) // Change the operation if last input was operation. if (IsBinOpCode(m_nLastCom)) { - int nPrev; bool fPrecInvToHigher = false; // Is Precedence Inversion from lower to higher precedence happening ?? m_nOpCode = (int)wParam; @@ -214,9 +205,9 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) // Here * is m_nPrevOpCode, m_currentVal is 2 (by 1*2), m_nLastCom is +, m_nOpCode is ^ if (m_fPrecedence && 0 != m_nPrevOpCode) { - nPrev = NPrecedenceOfOp(m_nPrevOpCode); - nx = NPrecedenceOfOp(m_nLastCom); - ni = NPrecedenceOfOp(m_nOpCode); + int nPrev = NPrecedenceOfOp(m_nPrevOpCode); + int nx = NPrecedenceOfOp(m_nLastCom); + int ni = NPrecedenceOfOp(m_nOpCode); if (nx <= nPrev && ni > nPrev) // condition for Precedence Inversion { fPrecInvToHigher = true; @@ -243,8 +234,8 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) { DoPrecedenceCheckAgain: - nx = NPrecedenceOfOp((int)wParam); - ni = NPrecedenceOfOp(m_nOpCode); + int nx = NPrecedenceOfOp((int)wParam); + int ni = NPrecedenceOfOp(m_nOpCode); if ((nx > ni) && m_fPrecedence) { @@ -492,8 +483,8 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) m_lastVal = m_precedenceVals[m_precedenceOpCount]; // Precedence Inversion check - ni = NPrecedenceOfOp(m_nPrevOpCode); - nx = NPrecedenceOfOp(m_nOpCode); + int ni = NPrecedenceOfOp(m_nPrevOpCode); + int nx = NPrecedenceOfOp(m_nOpCode); if (ni <= nx) { m_HistoryCollector.EnclosePrecInversionBrackets(); @@ -518,20 +509,15 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) case IDC_OPENP: case IDC_CLOSEP: - nx = 0; - if (wParam == IDC_OPENP) - { - nx = 1; - } // -IF- the Paren holding array is full and we try to add a paren // -OR- the paren holding array is empty and we try to remove a // paren // -OR- the precedence holding array is full - if ((m_openParenCount >= MAXPRECDEPTH && nx) || (!m_openParenCount && !nx) + if ((m_openParenCount >= MAXPRECDEPTH && (wParam == IDC_OPENP)) || (!m_openParenCount && (wParam != IDC_OPENP)) || ((m_precedenceOpCount >= MAXPRECDEPTH && m_nPrecOp[m_precedenceOpCount - 1] != 0))) { - if (!m_openParenCount && !nx) + if (!m_openParenCount && (wParam != IDC_OPENP)) { m_pCalcDisplay->OnNoRightParenAdded(); } @@ -540,7 +526,7 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) break; } - if (nx) + if (wParam == IDC_OPENP) { CheckAndAddLastBinOpToHistory(); m_HistoryCollector.AddOpenBraceToHistory(); @@ -588,8 +574,8 @@ void CCalcEngine::ProcessCommandWorker(OpCode wParam) for (m_nOpCode = m_nPrecOp[--m_precedenceOpCount]; m_nOpCode; m_nOpCode = m_nPrecOp[--m_precedenceOpCount]) { // Precedence Inversion check - ni = NPrecedenceOfOp(m_nPrevOpCode); - nx = NPrecedenceOfOp(m_nOpCode); + int ni = NPrecedenceOfOp(m_nPrevOpCode); + int nx = NPrecedenceOfOp(m_nOpCode); if (ni <= nx) { m_HistoryCollector.EnclosePrecInversionBrackets(); @@ -952,7 +938,7 @@ static const std::unordered_map operatorStringTable = { IDC_SECH, { SIDS_SECH, SIDS_ASECH } }, { IDC_CSCH, { SIDS_CSCH, SIDS_ACSCH } }, { IDC_COTH, { SIDS_COTH, SIDS_ACOTH } }, - + { IDC_LN, { L"", SIDS_POWE } }, { IDC_SQR, { SIDS_SQR } }, { IDC_CUB, { SIDS_CUBE } }, @@ -971,8 +957,8 @@ static const std::unordered_map operatorStringTable = { IDC_RSHFL, { SIDS_RSH } }, { IDC_RORC, { SIDS_ROR } }, { IDC_ROLC, { SIDS_ROL } }, - { IDC_CUBEROOT, {SIDS_CUBEROOT} }, - { IDC_MOD, {SIDS_MOD, L"", L"", L"", L"", L"", SIDS_PROGRAMMER_MOD} }, + { IDC_CUBEROOT, { SIDS_CUBEROOT } }, + { IDC_MOD, { SIDS_MOD, L"", L"", L"", L"", L"", SIDS_PROGRAMMER_MOD } }, }; wstring_view CCalcEngine::OpCodeToUnaryString(int nOpCode, bool fInv, ANGLE_TYPE angletype) diff --git a/src/CalcManager/CalculatorHistory.cpp b/src/CalcManager/CalculatorHistory.cpp index 6c9a002..384db07 100644 --- a/src/CalcManager/CalculatorHistory.cpp +++ b/src/CalcManager/CalculatorHistory.cpp @@ -41,7 +41,6 @@ unsigned int CalculatorHistory::AddToHistory( _In_ shared_ptr>> const& commands, wstring_view result) { - unsigned int addedIndex; shared_ptr spHistoryItem = make_shared(); spHistoryItem->historyItemVector.spTokens = tokens; @@ -53,9 +52,7 @@ unsigned int CalculatorHistory::AddToHistory( // in the history doesn't get broken for RTL languages spHistoryItem->historyItemVector.expression = L'\u202d' + generatedExpression + L'\u202c'; spHistoryItem->historyItemVector.result = wstring(result); - addedIndex = AddItem(spHistoryItem); - - return addedIndex; + return AddItem(spHistoryItem); } unsigned int CalculatorHistory::AddItem(_In_ shared_ptr const& spHistoryItem) @@ -66,19 +63,18 @@ unsigned int CalculatorHistory::AddItem(_In_ shared_ptr const& spHi } m_historyItems.push_back(spHistoryItem); - unsigned int lastIndex = static_cast(m_historyItems.size() - 1); - return lastIndex; + return static_cast(m_historyItems.size() - 1); } bool CalculatorHistory::RemoveItem(unsigned int uIdx) { - if (uIdx > m_historyItems.size() - 1) + if (uIdx < m_historyItems.size()) { - return false; + m_historyItems.erase(m_historyItems.begin() + uIdx); + return true; } - m_historyItems.erase(m_historyItems.begin() + uIdx); - return true; + return false; } vector> const& CalculatorHistory::GetHistory() diff --git a/src/CalcManager/Ratpack/conv.cpp b/src/CalcManager/Ratpack/conv.cpp index ab7aba0..5578b05 100644 --- a/src/CalcManager/Ratpack/conv.cpp +++ b/src/CalcManager/Ratpack/conv.cpp @@ -303,17 +303,13 @@ PRAT numtorat(_In_ PNUMBER pin, uint32_t radix) PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision) { - uint32_t bitmask; - uint32_t cdigits; - MANTTYPE* ptr; - PNUMBER sum = i32tonum(0, radix); PNUMBER powofnRadix = i32tonum(BASEX, radix); // A large penalty is paid for conversion of digits no one will see anyway. // limit the digits to the minimum of the existing precision or the // requested precision. - cdigits = precision + 1; + uint32_t cdigits = precision + 1; if (cdigits > (uint32_t)a->cdigit) { cdigits = (uint32_t)a->cdigit; @@ -323,10 +319,10 @@ PNUMBER nRadixxtonum(_In_ PNUMBER a, uint32_t radix, int32_t precision) numpowi32(&powofnRadix, a->exp + (a->cdigit - cdigits), radix, precision); // Loop over all the relative digits from MSD to LSD - for (ptr = &(a->mant[a->cdigit - 1]); cdigits > 0; ptr--, cdigits--) + for (MANTTYPE* ptr = &(a->mant[a->cdigit - 1]); cdigits > 0; ptr--, cdigits--) { // Loop over all the bits from MSB to LSB - for (bitmask = BASEX / 2; bitmask > 0; bitmask /= 2) + for (uint32_t bitmask = BASEX / 2; bitmask > 0; bitmask /= 2) { addnum(&sum, sum, radix); if (*ptr & bitmask) @@ -368,9 +364,7 @@ PNUMBER numtonRadixx(_In_ PNUMBER a, uint32_t radix) ptrdigit += a->cdigit - 1; PNUMBER thisdigit = nullptr; // thisdigit holds the current digit of a - // being summed into result. - int32_t idigit; // idigit is the iterate of digits in a. - for (idigit = 0; idigit < a->cdigit; idigit++) + for (int32_t idigit = 0; idigit < a->cdigit; idigit++) { mulnumx(&pnumret, num_radix); // WARNING: @@ -628,11 +622,10 @@ PNUMBER StringToNumber(wstring_view numberString, uint32_t radix, int32_t precis MANTTYPE* pmant = pnumret->mant + numberString.length() - 1; uint8_t state = START; // state is the state of the input state machine. - wchar_t curChar; for (const auto& c : numberString) { // If the character is the decimal separator, use L'.' for the purposes of the state machine. - curChar = (c == g_decimalSeparator ? L'.' : c); + wchar_t curChar = (c == g_decimalSeparator ? L'.' : c); // Switch states based on the character we encountered switch (curChar) @@ -1032,13 +1025,10 @@ int32_t numtoi32(_In_ PNUMBER pnum, uint32_t radix) bool stripzeroesnum(_Inout_ PNUMBER pnum, int32_t starting) { - MANTTYPE* pmant; - int32_t cdigits; bool fstrip = false; - // point pmant to the LeastCalculatedDigit - pmant = pnum->mant; - cdigits = pnum->cdigit; + MANTTYPE* pmant = pnum->mant; + int32_t cdigits = pnum->cdigit; // point pmant to the LSD if (cdigits > starting) { diff --git a/src/CalcManager/Ratpack/exp.cpp b/src/CalcManager/Ratpack/exp.cpp index 23257f7..b4020d4 100644 --- a/src/CalcManager/Ratpack/exp.cpp +++ b/src/CalcManager/Ratpack/exp.cpp @@ -63,7 +63,6 @@ void exprat(_Inout_ PRAT* px, uint32_t radix, int32_t precision) { PRAT pwr = nullptr; PRAT pint = nullptr; - int32_t intpwr; if (rat_gt(*px, rat_max_exp, precision) || rat_lt(*px, rat_min_exp, precision)) { @@ -76,7 +75,7 @@ void exprat(_Inout_ PRAT* px, uint32_t radix, int32_t precision) intrat(&pint, radix, precision); - intpwr = rattoi32(pint, radix, precision); + const int32_t intpwr = rattoi32(pint, radix, precision); ratpowi32(&pwr, intpwr, precision); subrat(px, pint, precision); @@ -153,7 +152,6 @@ void _lograt(PRAT* px, int32_t precision) void lograt(_Inout_ PRAT* px, int32_t precision) { - bool fneglog; PRAT pwr = nullptr; // pwr is the large scaling factor. PRAT offset = nullptr; // offset is the incremental scaling factor. @@ -164,12 +162,10 @@ void lograt(_Inout_ PRAT* px, int32_t precision) } // Get number > 1, for scaling - fneglog = rat_lt(*px, rat_one, precision); + bool fneglog = rat_lt(*px, rat_one, precision); if (fneglog) { - // WARNING: This is equivalent to doing *px = 1 / *px - PNUMBER pnumtemp = nullptr; - pnumtemp = (*px)->pp; + PNUMBER pnumtemp = (*px)->pp; (*px)->pp = (*px)->pq; (*px)->pq = pnumtemp; } @@ -178,10 +174,7 @@ void lograt(_Inout_ PRAT* px, int32_t precision) // log(x*2^(BASEXPWR*k)) = BASEXPWR*k*log(2)+log(x) if (LOGRAT2(*px) > 1) { - // Take advantage of px's base BASEX to scale quickly down to - // a reasonable range. - int32_t intpwr; - intpwr = LOGRAT2(*px) - 1; + const int32_t intpwr = LOGRAT2(*px) - 1; (*px)->pq->exp += intpwr; pwr = i32torat(intpwr * BASEXPWR); mulrat(&pwr, ln_two, precision); @@ -448,10 +441,9 @@ void powratcomp(_Inout_ PRAT* px, _In_ PRAT y, uint32_t radix, int32_t precision { // If power is an integer let ratpowi32 deal with it. PRAT iy = nullptr; - int32_t inty; DUPRAT(iy, y); subrat(&iy, podd, precision); - inty = rattoi32(iy, radix, precision); + int32_t inty = rattoi32(iy, radix, precision); PRAT plnx = nullptr; DUPRAT(plnx, *px); diff --git a/src/CalcManager/Ratpack/fact.cpp b/src/CalcManager/Ratpack/fact.cpp index c026e53..f0905e4 100644 --- a/src/CalcManager/Ratpack/fact.cpp +++ b/src/CalcManager/Ratpack/fact.cpp @@ -67,13 +67,9 @@ void _gamma(PRAT* pn, uint32_t radix, int32_t precision) PRAT sum = nullptr; PRAT err = nullptr; PRAT mpy = nullptr; - PRAT ratprec = nullptr; - PRAT ratRadix = nullptr; - int32_t oldprec; // Set up constants and initial conditions - oldprec = precision; - ratprec = i32torat(oldprec); + PRAT ratprec = i32torat(precision); // Find the best 'A' for convergence to the required precision. a = i32torat(radix); @@ -102,7 +98,7 @@ void _gamma(PRAT* pn, uint32_t radix, int32_t precision) exprat(&tmp, radix, precision); mulrat(&term, tmp, precision); lograt(&term, precision); - ratRadix = i32torat(radix); + const auto ratRadix = i32torat(radix); DUPRAT(tmp, ratRadix); lograt(&tmp, precision); subrat(&term, tmp, precision); @@ -173,7 +169,6 @@ void _gamma(PRAT* pn, uint32_t radix, int32_t precision) mulrat(&sum, mpy, precision); // And cleanup - precision = oldprec; destroyrat(ratprec); destroyrat(err); destroyrat(term); diff --git a/src/CalcManager/Ratpack/logic.cpp b/src/CalcManager/Ratpack/logic.cpp index c9094f3..197bda9 100644 --- a/src/CalcManager/Ratpack/logic.cpp +++ b/src/CalcManager/Ratpack/logic.cpp @@ -21,7 +21,6 @@ void lshrat(_Inout_ PRAT* pa, _In_ PRAT b, uint32_t radix, int32_t precision) { PRAT pwr = nullptr; - int32_t intb; intrat(pa, radix, precision); if (!zernum((*pa)->pp)) @@ -32,7 +31,7 @@ void lshrat(_Inout_ PRAT* pa, _In_ PRAT b, uint32_t radix, int32_t precision) // Don't attempt lsh of anything big throw(CALC_E_DOMAIN); } - intb = rattoi32(b, radix, precision); + const int32_t intb = rattoi32(b, radix, precision); DUPRAT(pwr, rat_two); ratpowi32(&pwr, intb, precision); mulrat(pa, pwr, precision); @@ -44,7 +43,6 @@ void rshrat(_Inout_ PRAT* pa, _In_ PRAT b, uint32_t radix, int32_t precision) { PRAT pwr = nullptr; - int32_t intb; intrat(pa, radix, precision); if (!zernum((*pa)->pp)) @@ -55,7 +53,7 @@ void rshrat(_Inout_ PRAT* pa, _In_ PRAT b, uint32_t radix, int32_t precision) // Don't attempt rsh of anything big and negative. throw(CALC_E_DOMAIN); } - intb = rattoi32(b, radix, precision); + const int32_t intb = rattoi32(b, radix, precision); DUPRAT(pwr, rat_two); ratpowi32(&pwr, intb, precision); divrat(pa, pwr, precision); diff --git a/src/CalcManager/Ratpack/num.cpp b/src/CalcManager/Ratpack/num.cpp index c456105..6055e41 100644 --- a/src/CalcManager/Ratpack/num.cpp +++ b/src/CalcManager/Ratpack/num.cpp @@ -67,8 +67,7 @@ void _addnum(PNUMBER* pa, PNUMBER b, uint32_t radix) MANTTYPE* pcha; // pcha is a pointer to the mantissa of a. MANTTYPE* pchb; // pchb is a pointer to the mantissa of b. MANTTYPE* pchc; // pchc is a pointer to the mantissa of c. - int32_t cdigits; // cdigits is the max count of the digits results - // used as a counter. + int32_t cdigits; // cdigits is the max count of the digits results used as a counter. int32_t mexp; // mexp is the exponent of the result. MANTTYPE da; // da is a single 'digit' after possible padding. MANTTYPE db; // db is a single 'digit' after possible padding. @@ -558,48 +557,34 @@ bool equnum(_In_ PNUMBER a, _In_ PNUMBER b) bool lessnum(_In_ PNUMBER a, _In_ PNUMBER b) { - int32_t diff; - MANTTYPE* pa; - MANTTYPE* pb; - int32_t cdigits; - int32_t ccdigits; - MANTTYPE da; - MANTTYPE db; - - diff = (a->cdigit + a->exp) - (b->cdigit + b->exp); + int32_t diff = (a->cdigit + a->exp) - (b->cdigit + b->exp); if (diff < 0) { // The exponent of a is less than b return true; } - else + if (diff > 0) { - if (diff > 0) + return false; + } + MANTTYPE* pa = a->mant; + MANTTYPE* pb = b->mant; + pa += a->cdigit - 1; + pb += b->cdigit - 1; + int32_t cdigits = max(a->cdigit, b->cdigit); + int32_t ccdigits = cdigits; + for (; cdigits > 0; cdigits--) + { + MANTTYPE da = ((cdigits > (ccdigits - a->cdigit)) ? *pa-- : 0); + MANTTYPE db = ((cdigits > (ccdigits - b->cdigit)) ? *pb-- : 0); + diff = da - db; + if (diff) { - return false; - } - else - { - pa = a->mant; - pb = b->mant; - pa += a->cdigit - 1; - pb += b->cdigit - 1; - cdigits = max(a->cdigit, b->cdigit); - ccdigits = cdigits; - for (; cdigits > 0; cdigits--) - { - da = ((cdigits > (ccdigits - a->cdigit)) ? *pa-- : 0); - db = ((cdigits > (ccdigits - b->cdigit)) ? *pb-- : 0); - diff = da - db; - if (diff) - { - return (diff < 0); - } - } - // In this case, they are equal. - return false; + return (diff < 0); } } + // In this case, they are equal. + return false; } //---------------------------------------------------------------------------- diff --git a/src/CalcManager/Ratpack/support.cpp b/src/CalcManager/Ratpack/support.cpp index 81c737f..ef9e410 100644 --- a/src/CalcManager/Ratpack/support.cpp +++ b/src/CalcManager/Ratpack/support.cpp @@ -596,15 +596,13 @@ void _dumprawrat(_In_ const wchar_t* varname, _In_ PRAT rat, wostream& out) void _dumprawnum(_In_ const wchar_t* varname, _In_ PNUMBER num, wostream& out) { - int i; - out << L"NUMBER " << varname << L" = {\n"; out << L"\t" << num->sign << L",\n"; out << L"\t" << num->cdigit << L",\n"; out << L"\t" << num->exp << L",\n"; out << L"\t{ "; - for (i = 0; i < num->cdigit; i++) + for (int i = 0; i < num->cdigit; i++) { out << L" " << num->mant[i] << L","; } @@ -681,10 +679,9 @@ void trimit(_Inout_ PRAT* px, int32_t precision) { if (!g_ftrueinfinite) { - int32_t trim; PNUMBER pp = (*px)->pp; PNUMBER pq = (*px)->pq; - trim = g_ratio * (min((pp->cdigit + pp->exp), (pq->cdigit + pq->exp)) - 1) - precision; + int32_t trim = g_ratio * (min((pp->cdigit + pp->exp), (pq->cdigit + pq->exp)) - 1) - precision; if (trim > g_ratio) { trim /= g_ratio;