From 347e18a3e8a742dc63833bc030dcf224499853b3 Mon Sep 17 00:00:00 2001 From: Mike Kaganski Date: Mon, 4 Oct 2021 14:15:06 +0200 Subject: Use isRepresentableInteger here Change-Id: I8ba202232fa42765a8b04113639fdac4b5724aa2 Reviewed-on: https://gerrit.libreoffice.org/c/core/+/122941 Tested-by: Jenkins Reviewed-by: Mike Kaganski --- sal/rtl/math.cxx | 100 ++++++++++++++++++++++++++----------------------------- 1 file changed, 47 insertions(+), 53 deletions(-) (limited to 'sal') diff --git a/sal/rtl/math.cxx b/sal/rtl/math.cxx index f6a2b15a21d4..4d1111590089 100644 --- a/sal/rtl/math.cxx +++ b/sal/rtl/math.cxx @@ -335,71 +335,65 @@ void doubleToString(typename T::String ** pResult, // Use integer representation for integer values that fit into the // mantissa (1.((2^53)-1)) with a precision of 1 for highest accuracy. - const sal_Int64 kMaxInt = (static_cast< sal_Int64 >(1) << 53) - 1; if ((eFormat == rtl_math_StringFormat_Automatic || - eFormat == rtl_math_StringFormat_F) && fValue <= static_cast< double >(kMaxInt)) + eFormat == rtl_math_StringFormat_F) && isRepresentableInteger(fValue)) { sal_Int64 nInt = static_cast< sal_Int64 >(fValue); - // Check the integer range again because double comparison may yield - // true within the precision range. - if (nInt <= kMaxInt && static_cast< double >(nInt) == fValue) - { - if (nDecPlaces == rtl_math_DecimalPlaces_Max) - nDecPlaces = 0; - else - nDecPlaces = ::std::clamp< sal_Int32 >(nDecPlaces, -15, 15); + if (nDecPlaces == rtl_math_DecimalPlaces_Max) + nDecPlaces = 0; + else + nDecPlaces = ::std::clamp< sal_Int32 >(nDecPlaces, -15, 15); - if (bEraseTrailingDecZeros && nDecPlaces > 0) - nDecPlaces = 0; + if (bEraseTrailingDecZeros && nDecPlaces > 0) + nDecPlaces = 0; - // Round before decimal position. - if (nDecPlaces < 0) - { - sal_Int64 nRounding = static_cast< sal_Int64 >(getN10Exp(-nDecPlaces - 1)); - const sal_Int64 nTemp = (nInt / nRounding + 5) / 10; - nInt = nTemp * 10 * nRounding; - } + // Round before decimal position. + if (nDecPlaces < 0) + { + sal_Int64 nRounding = static_cast< sal_Int64 >(getN10Exp(-nDecPlaces - 1)); + const sal_Int64 nTemp = (nInt / nRounding + 5) / 10; + nInt = nTemp * 10 * nRounding; + } - // Max 1 sign, 16 integer digits, 15 group separators, 1 decimal - // separator, 15 decimals digits. - typename T::Char aBuf[64]; - typename T::Char* pEnd = aBuf + 40; - typename T::Char* pStart = pEnd; + // Max 1 sign, 16 integer digits, 15 group separators, 1 decimal + // separator, 15 decimals digits. + typename T::Char aBuf[64]; + typename T::Char* pEnd = aBuf + 40; + typename T::Char* pStart = pEnd; - // Backward fill. - sal_Int32 nGrouping = cGroupSeparator && pGroups ? *pGroups : 0; - sal_Int32 nGroupDigits = 0; - do + // Backward fill. + sal_Int32 nGrouping = cGroupSeparator && pGroups ? *pGroups : 0; + sal_Int32 nGroupDigits = 0; + do + { + typename T::Char nDigit = nInt % 10; + nInt /= 10; + *--pStart = nDigit + '0'; + if (nGrouping && nGrouping == ++nGroupDigits && nInt) { - typename T::Char nDigit = nInt % 10; - nInt /= 10; - *--pStart = nDigit + '0'; - if (nGrouping && nGrouping == ++nGroupDigits && nInt) - { - *--pStart = cGroupSeparator; - if (*(pGroups + 1)) - nGrouping = *++pGroups; - nGroupDigits = 0; - } + *--pStart = cGroupSeparator; + if (*(pGroups + 1)) + nGrouping = *++pGroups; + nGroupDigits = 0; } - while (nInt); - if (bSign) - *--pStart = '-'; + } + while (nInt); + if (bSign) + *--pStart = '-'; - // Append decimals. - if (nDecPlaces > 0) - { - *pEnd++ = cDecSeparator; - pEnd = std::fill_n(pEnd, nDecPlaces, '0'); - } + // Append decimals. + if (nDecPlaces > 0) + { + *pEnd++ = cDecSeparator; + pEnd = std::fill_n(pEnd, nDecPlaces, '0'); + } - if (!pResultCapacity) - T::createString(pResult, pStart, pEnd - pStart); - else - T::appendChars(pResult, pResultCapacity, &nResultOffset, pStart, pEnd - pStart); + if (!pResultCapacity) + T::createString(pResult, pStart, pEnd - pStart); + else + T::appendChars(pResult, pResultCapacity, &nResultOffset, pStart, pEnd - pStart); - return; - } + return; } // find the exponent -- cgit