From c1fefd3a7b2f4b7161301bc9ce4d5880348dd20d Mon Sep 17 00:00:00 2001 From: pi1024e <49824824+pi1024e@users.noreply.github.com> Date: Mon, 18 May 2020 23:14:40 -0400 Subject: [PATCH] Change precision and variables regarding the number of digits to be unsigned instead of signed (#1092) --- src/CalcManager/NumberFormattingUtils.cpp | 6 ++-- src/CalcManager/NumberFormattingUtils.h | 2 +- src/CalcManager/UnitConverter.cpp | 44 +++++++++++------------ 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/src/CalcManager/NumberFormattingUtils.cpp b/src/CalcManager/NumberFormattingUtils.cpp index ead9013f..f535cecb 100644 --- a/src/CalcManager/NumberFormattingUtils.cpp +++ b/src/CalcManager/NumberFormattingUtils.cpp @@ -50,15 +50,15 @@ namespace CalcManager::NumberFormattingUtils /// the number unsigned int GetNumberDigitsWholeNumberPart(double value) { - return value == 0 ? 1 : (1 + (int)log10(abs(value))); + return value == 0 ? 1 : (1 + static_cast(log10(abs(value)))); } /// /// Rounds the given double to the given number of significant digits /// /// input double - /// int number of significant digits to round to - wstring RoundSignificantDigits(double num, int numSignificant) + /// unsigned int number of significant digits to round to + wstring RoundSignificantDigits(double num, unsigned int numSignificant) { wstringstream out(wstringstream::out); out << fixed; diff --git a/src/CalcManager/NumberFormattingUtils.h b/src/CalcManager/NumberFormattingUtils.h index ab337eed..2f13ebfc 100644 --- a/src/CalcManager/NumberFormattingUtils.h +++ b/src/CalcManager/NumberFormattingUtils.h @@ -10,6 +10,6 @@ namespace CalcManager::NumberFormattingUtils void TrimTrailingZeros(_Inout_ std::wstring& input); unsigned int GetNumberDigits(std::wstring value); unsigned int GetNumberDigitsWholeNumberPart(double value); - std::wstring RoundSignificantDigits(double value, int numberSignificantDigits); + std::wstring RoundSignificantDigits(double value, unsigned int numberSignificantDigits); std::wstring ToScientificNumber(double number); } diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index 05242a08..f7a413b7 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -13,19 +13,19 @@ using namespace std; using namespace UnitConversionManager; using namespace CalcManager::NumberFormattingUtils; -static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3; -static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6; -static constexpr uint32_t EXPECTEDSTATEDATATOKENCOUNT = 5; -static constexpr uint32_t EXPECTEDMAPCOMPONENTTOKENCOUNT = 2; +static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3U; +static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6U; +static constexpr uint32_t EXPECTEDSTATEDATATOKENCOUNT = 5U; +static constexpr uint32_t EXPECTEDMAPCOMPONENTTOKENCOUNT = 2U; -static constexpr int32_t MAXIMUMDIGITSALLOWED = 15; -static constexpr int32_t OPTIMALDIGITSALLOWED = 7; +static constexpr uint32_t MAXIMUMDIGITSALLOWED = 15U; +static constexpr uint32_t OPTIMALDIGITSALLOWED = 7U; static constexpr wchar_t LEFTESCAPECHAR = L'{'; static constexpr wchar_t RIGHTESCAPECHAR = L'}'; -static const double OPTIMALDECIMALALLOWED = pow(10, -1 * (OPTIMALDIGITSALLOWED - 1)); -static const double MINIMUMDECIMALALLOWED = pow(10, -1 * (MAXIMUMDIGITSALLOWED - 1)); +static const double OPTIMALDECIMALALLOWED = 1e-6; // pow(10, -1 * (OPTIMALDIGITSALLOWED - 1)); +static const double MINIMUMDECIMALALLOWED = 1e-14; // pow(10, -1 * (MAXIMUMDIGITSALLOWED - 1)); unordered_map quoteConversions; unordered_map unquoteConversions; @@ -643,15 +643,15 @@ vector> UnitConverter::CalculateSuggested() wstring roundedString; if (abs(entry.value) < 100) { - roundedString = RoundSignificantDigits(entry.value, 2); + roundedString = RoundSignificantDigits(entry.value, 2U); } else if (abs(entry.value) < 1000) { - roundedString = RoundSignificantDigits(entry.value, 1); + roundedString = RoundSignificantDigits(entry.value, 1U); } else { - roundedString = RoundSignificantDigits(entry.value, 0); + roundedString = RoundSignificantDigits(entry.value, 0U); } if (stod(roundedString) != 0.0 || m_currentCategory.supportsNegative) { @@ -681,15 +681,15 @@ vector> UnitConverter::CalculateSuggested() wstring roundedString; if (abs(entry.value) < 100) { - roundedString = RoundSignificantDigits(entry.value, 2); + roundedString = RoundSignificantDigits(entry.value, 2U); } else if (abs(entry.value) < 1000) { - roundedString = RoundSignificantDigits(entry.value, 1); + roundedString = RoundSignificantDigits(entry.value, 1U); } else { - roundedString = RoundSignificantDigits(entry.value, 0); + roundedString = RoundSignificantDigits(entry.value, 0U); } // How to work out which is the best whimsical value to add to the vector? @@ -800,8 +800,8 @@ void UnitConverter::InitializeSelectedUnits() { // Units may already have been initialized through UnitConverter::RestoreUserPreferences(). // Check if they have been, and if so, do not override restored units. - bool isFromUnitValid = m_fromType != EMPTY_UNIT && find(curUnits.begin(), curUnits.end(), m_fromType) != curUnits.end(); - bool isToUnitValid = m_toType != EMPTY_UNIT && find(curUnits.begin(), curUnits.end(), m_toType) != curUnits.end(); + const bool isFromUnitValid = m_fromType != EMPTY_UNIT && find(curUnits.begin(), curUnits.end(), m_fromType) != curUnits.end(); + const bool isToUnitValid = m_toType != EMPTY_UNIT && find(curUnits.begin(), curUnits.end(), m_toType) != curUnits.end(); if (isFromUnitValid && isToUnitValid) { @@ -877,9 +877,9 @@ void UnitConverter::Calculate() else { double currentValue = stod(m_currentDisplay); - double returnValue = Convert(currentValue, conversionTable[m_toType]); + const double returnValue = Convert(currentValue, conversionTable[m_toType]); - auto isCurrencyConverter = m_currencyDataLoader != nullptr && m_currencyDataLoader->SupportsCategory(this->m_currentCategory); + const auto isCurrencyConverter = m_currencyDataLoader != nullptr && m_currencyDataLoader->SupportsCategory(this->m_currentCategory); if (isCurrencyConverter) { // We don't need to trim the value when it's a currency. @@ -888,15 +888,15 @@ void UnitConverter::Calculate() } else { - int numPreDecimal = GetNumberDigitsWholeNumberPart(returnValue); + const unsigned int numPreDecimal = GetNumberDigitsWholeNumberPart(returnValue); if (numPreDecimal > MAXIMUMDIGITSALLOWED || (returnValue != 0 && abs(returnValue) < MINIMUMDECIMALALLOWED)) { m_returnDisplay = ToScientificNumber(returnValue); } else { - int currentNumberSignificantDigits = GetNumberDigits(m_currentDisplay); - int precision; + const unsigned int currentNumberSignificantDigits = GetNumberDigits(m_currentDisplay); + unsigned int precision; if (abs(returnValue) < OPTIMALDECIMALALLOWED) { precision = MAXIMUMDIGITSALLOWED; @@ -905,7 +905,7 @@ void UnitConverter::Calculate() { // Fewer digits are needed following the decimal if the number is large, // we calculate the number of decimals necessary based on the number of digits in the integer part. - precision = max(0, max(OPTIMALDIGITSALLOWED, min(MAXIMUMDIGITSALLOWED, currentNumberSignificantDigits)) - numPreDecimal); + precision = max(0U, max(OPTIMALDIGITSALLOWED, min(MAXIMUMDIGITSALLOWED, currentNumberSignificantDigits)) - numPreDecimal); } m_returnDisplay = RoundSignificantDigits(returnValue, precision);