Fix how we calculate the precision in Unit converter and update GetNumberDigitsWholeNumberPart (#1256)

* Fix 1255

* optimization

* spacing
This commit is contained in:
Rudy Huyn 2020-06-01 16:50:45 -07:00 committed by GitHub
parent 60a7ee3604
commit 75fde82f46
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 7 additions and 2 deletions

View File

@ -50,7 +50,7 @@ namespace CalcManager::NumberFormattingUtils
/// <param name="value">the number</param>
unsigned int GetNumberDigitsWholeNumberPart(double value)
{
return value == 0 ? 1 : (1 + static_cast<unsigned int>(log10(abs(value))));
return value == 0 ? 1u : static_cast<unsigned int>(1 + max(0.0, log10(abs(value))));
}
/// <summary>

View File

@ -905,7 +905,8 @@ 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(0U, max(OPTIMALDIGITSALLOWED, min(MAXIMUMDIGITSALLOWED, currentNumberSignificantDigits)) - numPreDecimal);
auto numberDigits = max(OPTIMALDIGITSALLOWED, min(MAXIMUMDIGITSALLOWED, currentNumberSignificantDigits));
precision = numberDigits > numPreDecimal ? numberDigits - numPreDecimal : 0;
}
m_returnDisplay = RoundSignificantDigits(returnValue, precision);

View File

@ -970,6 +970,10 @@ namespace CalculatorManagerTest
VERIFY_ARE_EQUAL(digitsCount, 15);
digitsCount = GetNumberDigitsWholeNumberPart(324328412837382.232213214324234);
VERIFY_ARE_EQUAL(digitsCount, 15);
digitsCount = GetNumberDigitsWholeNumberPart(0.032);
VERIFY_ARE_EQUAL(digitsCount, 1);
digitsCount = GetNumberDigitsWholeNumberPart(0.00000000000000000001);
VERIFY_ARE_EQUAL(digitsCount, 1);
}
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_RoundSignificantDigits()