Change precision and variables regarding the number of digits to be unsigned instead of signed (#1092)

This commit is contained in:
pi1024e 2020-05-18 23:14:40 -04:00 committed by GitHub
parent a69ee94663
commit c1fefd3a7b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 26 additions and 26 deletions

View File

@ -50,15 +50,15 @@ namespace CalcManager::NumberFormattingUtils
/// <param name="value">the number</param> /// <param name="value">the number</param>
unsigned int GetNumberDigitsWholeNumberPart(double value) unsigned int GetNumberDigitsWholeNumberPart(double value)
{ {
return value == 0 ? 1 : (1 + (int)log10(abs(value))); return value == 0 ? 1 : (1 + static_cast<unsigned int>(log10(abs(value))));
} }
/// <summary> /// <summary>
/// Rounds the given double to the given number of significant digits /// Rounds the given double to the given number of significant digits
/// </summary> /// </summary>
/// <param name="num">input double</param> /// <param name="num">input double</param>
/// <param name="numSignificant">int number of significant digits to round to</param> /// <param name="numSignificant">unsigned int number of significant digits to round to</param>
wstring RoundSignificantDigits(double num, int numSignificant) wstring RoundSignificantDigits(double num, unsigned int numSignificant)
{ {
wstringstream out(wstringstream::out); wstringstream out(wstringstream::out);
out << fixed; out << fixed;

View File

@ -10,6 +10,6 @@ namespace CalcManager::NumberFormattingUtils
void TrimTrailingZeros(_Inout_ std::wstring& input); void TrimTrailingZeros(_Inout_ std::wstring& input);
unsigned int GetNumberDigits(std::wstring value); unsigned int GetNumberDigits(std::wstring value);
unsigned int GetNumberDigitsWholeNumberPart(double 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); std::wstring ToScientificNumber(double number);
} }

View File

@ -13,19 +13,19 @@ using namespace std;
using namespace UnitConversionManager; using namespace UnitConversionManager;
using namespace CalcManager::NumberFormattingUtils; using namespace CalcManager::NumberFormattingUtils;
static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3; static constexpr uint32_t EXPECTEDSERIALIZEDCATEGORYTOKENCOUNT = 3U;
static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6; static constexpr uint32_t EXPECTEDSERIALIZEDUNITTOKENCOUNT = 6U;
static constexpr uint32_t EXPECTEDSTATEDATATOKENCOUNT = 5; static constexpr uint32_t EXPECTEDSTATEDATATOKENCOUNT = 5U;
static constexpr uint32_t EXPECTEDMAPCOMPONENTTOKENCOUNT = 2; static constexpr uint32_t EXPECTEDMAPCOMPONENTTOKENCOUNT = 2U;
static constexpr int32_t MAXIMUMDIGITSALLOWED = 15; static constexpr uint32_t MAXIMUMDIGITSALLOWED = 15U;
static constexpr int32_t OPTIMALDIGITSALLOWED = 7; static constexpr uint32_t OPTIMALDIGITSALLOWED = 7U;
static constexpr wchar_t LEFTESCAPECHAR = L'{'; static constexpr wchar_t LEFTESCAPECHAR = L'{';
static constexpr wchar_t RIGHTESCAPECHAR = L'}'; static constexpr wchar_t RIGHTESCAPECHAR = L'}';
static const double OPTIMALDECIMALALLOWED = pow(10, -1 * (OPTIMALDIGITSALLOWED - 1)); static const double OPTIMALDECIMALALLOWED = 1e-6; // pow(10, -1 * (OPTIMALDIGITSALLOWED - 1));
static const double MINIMUMDECIMALALLOWED = pow(10, -1 * (MAXIMUMDIGITSALLOWED - 1)); static const double MINIMUMDECIMALALLOWED = 1e-14; // pow(10, -1 * (MAXIMUMDIGITSALLOWED - 1));
unordered_map<wchar_t, wstring> quoteConversions; unordered_map<wchar_t, wstring> quoteConversions;
unordered_map<wstring, wchar_t> unquoteConversions; unordered_map<wstring, wchar_t> unquoteConversions;
@ -643,15 +643,15 @@ vector<tuple<wstring, Unit>> UnitConverter::CalculateSuggested()
wstring roundedString; wstring roundedString;
if (abs(entry.value) < 100) if (abs(entry.value) < 100)
{ {
roundedString = RoundSignificantDigits(entry.value, 2); roundedString = RoundSignificantDigits(entry.value, 2U);
} }
else if (abs(entry.value) < 1000) else if (abs(entry.value) < 1000)
{ {
roundedString = RoundSignificantDigits(entry.value, 1); roundedString = RoundSignificantDigits(entry.value, 1U);
} }
else else
{ {
roundedString = RoundSignificantDigits(entry.value, 0); roundedString = RoundSignificantDigits(entry.value, 0U);
} }
if (stod(roundedString) != 0.0 || m_currentCategory.supportsNegative) if (stod(roundedString) != 0.0 || m_currentCategory.supportsNegative)
{ {
@ -681,15 +681,15 @@ vector<tuple<wstring, Unit>> UnitConverter::CalculateSuggested()
wstring roundedString; wstring roundedString;
if (abs(entry.value) < 100) if (abs(entry.value) < 100)
{ {
roundedString = RoundSignificantDigits(entry.value, 2); roundedString = RoundSignificantDigits(entry.value, 2U);
} }
else if (abs(entry.value) < 1000) else if (abs(entry.value) < 1000)
{ {
roundedString = RoundSignificantDigits(entry.value, 1); roundedString = RoundSignificantDigits(entry.value, 1U);
} }
else 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? // 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(). // Units may already have been initialized through UnitConverter::RestoreUserPreferences().
// Check if they have been, and if so, do not override restored units. // 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(); const 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 isToUnitValid = m_toType != EMPTY_UNIT && find(curUnits.begin(), curUnits.end(), m_toType) != curUnits.end();
if (isFromUnitValid && isToUnitValid) if (isFromUnitValid && isToUnitValid)
{ {
@ -877,9 +877,9 @@ void UnitConverter::Calculate()
else else
{ {
double currentValue = stod(m_currentDisplay); 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) if (isCurrencyConverter)
{ {
// We don't need to trim the value when it's a currency. // We don't need to trim the value when it's a currency.
@ -888,15 +888,15 @@ void UnitConverter::Calculate()
} }
else else
{ {
int numPreDecimal = GetNumberDigitsWholeNumberPart(returnValue); const unsigned int numPreDecimal = GetNumberDigitsWholeNumberPart(returnValue);
if (numPreDecimal > MAXIMUMDIGITSALLOWED || (returnValue != 0 && abs(returnValue) < MINIMUMDECIMALALLOWED)) if (numPreDecimal > MAXIMUMDIGITSALLOWED || (returnValue != 0 && abs(returnValue) < MINIMUMDECIMALALLOWED))
{ {
m_returnDisplay = ToScientificNumber(returnValue); m_returnDisplay = ToScientificNumber(returnValue);
} }
else else
{ {
int currentNumberSignificantDigits = GetNumberDigits(m_currentDisplay); const unsigned int currentNumberSignificantDigits = GetNumberDigits(m_currentDisplay);
int precision; unsigned int precision;
if (abs(returnValue) < OPTIMALDECIMALALLOWED) if (abs(returnValue) < OPTIMALDECIMALALLOWED)
{ {
precision = MAXIMUMDIGITSALLOWED; precision = MAXIMUMDIGITSALLOWED;
@ -905,7 +905,7 @@ void UnitConverter::Calculate()
{ {
// Fewer digits are needed following the decimal if the number is large, // 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. // 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); m_returnDisplay = RoundSignificantDigits(returnValue, precision);