mirror of
https://github.com/Microsoft/calculator.git
synced 2025-08-20 21:33:10 -07:00
Fix how we calculate the precision in Unit converter and update GetNumberDigitsWholeNumberPart (#1256)
* Fix 1255 * optimization * spacing
This commit is contained in:
parent
60a7ee3604
commit
75fde82f46
3 changed files with 7 additions and 2 deletions
|
@ -50,7 +50,7 @@ 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 + static_cast<unsigned int>(log10(abs(value))));
|
return value == 0 ? 1u : static_cast<unsigned int>(1 + max(0.0, log10(abs(value))));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
|
|
|
@ -905,7 +905,8 @@ 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(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);
|
m_returnDisplay = RoundSignificantDigits(returnValue, precision);
|
||||||
|
|
|
@ -970,6 +970,10 @@ namespace CalculatorManagerTest
|
||||||
VERIFY_ARE_EQUAL(digitsCount, 15);
|
VERIFY_ARE_EQUAL(digitsCount, 15);
|
||||||
digitsCount = GetNumberDigitsWholeNumberPart(324328412837382.232213214324234);
|
digitsCount = GetNumberDigitsWholeNumberPart(324328412837382.232213214324234);
|
||||||
VERIFY_ARE_EQUAL(digitsCount, 15);
|
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()
|
void CalculatorManagerTest::CalculatorManagerNumberFormattingUtils_RoundSignificantDigits()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue