From 75fde82f4613d5fe63e2fb2ddc2e57c80309933b Mon Sep 17 00:00:00 2001 From: Rudy Huyn Date: Mon, 1 Jun 2020 16:50:45 -0700 Subject: [PATCH] Fix how we calculate the precision in Unit converter and update GetNumberDigitsWholeNumberPart (#1256) * Fix 1255 * optimization * spacing --- src/CalcManager/NumberFormattingUtils.cpp | 2 +- src/CalcManager/UnitConverter.cpp | 3 ++- src/CalculatorUnitTests/CalculatorManagerTest.cpp | 4 ++++ 3 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/CalcManager/NumberFormattingUtils.cpp b/src/CalcManager/NumberFormattingUtils.cpp index f535cecb..63563972 100644 --- a/src/CalcManager/NumberFormattingUtils.cpp +++ b/src/CalcManager/NumberFormattingUtils.cpp @@ -50,7 +50,7 @@ namespace CalcManager::NumberFormattingUtils /// the number unsigned int GetNumberDigitsWholeNumberPart(double value) { - return value == 0 ? 1 : (1 + static_cast(log10(abs(value)))); + return value == 0 ? 1u : static_cast(1 + max(0.0, log10(abs(value)))); } /// diff --git a/src/CalcManager/UnitConverter.cpp b/src/CalcManager/UnitConverter.cpp index f7a413b7..ec85e57b 100644 --- a/src/CalcManager/UnitConverter.cpp +++ b/src/CalcManager/UnitConverter.cpp @@ -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); diff --git a/src/CalculatorUnitTests/CalculatorManagerTest.cpp b/src/CalculatorUnitTests/CalculatorManagerTest.cpp index ab8e83e8..eb726405 100644 --- a/src/CalculatorUnitTests/CalculatorManagerTest.cpp +++ b/src/CalculatorUnitTests/CalculatorManagerTest.cpp @@ -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()