diff options
Diffstat (limited to '')
-rw-r--r-- | include/rtl/math.h | 509 | ||||
-rw-r--r-- | include/rtl/math.hxx | 511 |
2 files changed, 1020 insertions, 0 deletions
diff --git a/include/rtl/math.h b/include/rtl/math.h new file mode 100644 index 0000000000..1938f037c7 --- /dev/null +++ b/include/rtl/math.h @@ -0,0 +1,509 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +/* + * This file is part of LibreOffice published API. + */ + +#ifndef INCLUDED_RTL_MATH_H +#define INCLUDED_RTL_MATH_H + +#include "sal/config.h" + +#include "rtl/ustring.h" +#include "sal/saldllapi.h" +#include "sal/types.h" + +#if defined __cplusplus +extern "C" { +#endif /* __cplusplus */ + +/** Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString + and rtl_math_doubleToUStringBuffer. + */ +enum rtl_math_StringFormat +{ + /** Like sprintf() %E. + */ + rtl_math_StringFormat_E, + + /** Like sprintf() %f. + */ + rtl_math_StringFormat_F, + + /** Like sprintf() %G, 'F' or 'E' format is used depending on which one is + more compact. + */ + rtl_math_StringFormat_G, + + /** Automatic, 'F' or 'E' format is used depending on the numeric value to + be formatted. + */ + rtl_math_StringFormat_Automatic, + + /** Same 'E', but with only 1 minimum digits in exponent. + @since LibreOffice 5.0 + */ + rtl_math_StringFormat_E1, + + /** Same 'E', but with only 2 minimum digits in exponent. + @since LibreOffice 5.0 + */ + rtl_math_StringFormat_E2, + + /** Same 'G', but with only 1 minimum digits in exponent. + @since LibreOffice 5.0 + */ + rtl_math_StringFormat_G1, + + /** Same 'G', but with only 2 minimum digits in exponent. + @since LibreOffice 5.0 + */ + rtl_math_StringFormat_G2, + + /** @cond INTERNAL */ + rtl_math_StringFormat_FORCE_EQUAL_SIZE = SAL_MAX_ENUM + /** @endcond */ +}; + +/** Status for rtl_math_stringToDouble and rtl_math_uStringToDouble. + */ +enum rtl_math_ConversionStatus +{ + /** Conversion was successful. + */ + rtl_math_ConversionStatus_Ok, + + /** Conversion caused overflow or underflow. + */ + rtl_math_ConversionStatus_OutOfRange, + + /** @cond INTERNAL */ + rtl_math_ConversionStatus_FORCE_EQUAL_SIZE = SAL_MAX_ENUM + /** @endcond */ +}; + +/** Rounding modes for rtl_math_round. + */ +enum rtl_math_RoundingMode +{ + /** Like HalfUp, but corrects roundoff errors, preferred. + */ + rtl_math_RoundingMode_Corrected, + + /** Floor of absolute value, signed return (commercial). + */ + rtl_math_RoundingMode_Down, + + /** Ceil of absolute value, signed return (commercial). + */ + rtl_math_RoundingMode_Up, + + /** Floor of signed value. + */ + rtl_math_RoundingMode_Floor, + + /** Ceil of signed value. + */ + rtl_math_RoundingMode_Ceiling, + + /** Frac <= 0.5 ? floor of abs : ceil of abs, signed return. + */ + rtl_math_RoundingMode_HalfDown, + + /** Frac < 0.5 ? floor of abs : ceil of abs, signed return (mathematical). + */ + rtl_math_RoundingMode_HalfUp, + + /** IEEE rounding mode (statistical). + */ + rtl_math_RoundingMode_HalfEven, + + /** @cond INTERNAL */ + rtl_math_RoundingMode_FORCE_EQUAL_SIZE = SAL_MAX_ENUM + /** @endcond */ +}; + +/** Special decimal places constants for rtl_math_doubleToString and + rtl_math_doubleToUString and rtl_math_doubleToUStringBuffer. + */ +enum rtl_math_DecimalPlaces +{ + /** Value to be used with rtl_math_StringFormat_Automatic. + */ + rtl_math_DecimalPlaces_Max = 0x7ffffff, + + /** Value to be used with rtl_math_StringFormat_G. + In fact the same value as rtl_math_DecimalPlaces_Max, just an alias for + better understanding. + */ + rtl_math_DecimalPlaces_DefaultSignificance = rtl_math_DecimalPlaces_Max +}; + + +/** Conversions analogous to sprintf() using internal rounding. + + +/-HUGE_VAL are converted to "INF" and "-INF", NAN values are + converted to "NaN". + + @param pResult + Returns the resulting byte string. Must itself not be null, and must point + to either null or a valid string. + + @param pResultCapacity + If null, pResult is considered to point to immutable strings, and a new + string will be allocated in pResult. + If non-null, it points to the current capacity of pResult, which is + considered to point to a string buffer (pResult must not itself be null in + this case, and must point to a string that has room for the given capacity). + The string representation of the given double value is inserted into pResult + at position nResultOffset. If pResult's current capacity is too small, a + new string buffer will be allocated in pResult as necessary, and + pResultCapacity will contain the new capacity on return. + + @param nResultOffset + If pResult is used as a string buffer (i.e., pResultCapacity is non-null), + nResultOffset specifies the insertion offset within the buffer. Ignored + otherwise. + + @param fValue + The value to convert. + + @param eFormat + The format to use, one of rtl_math_StringFormat. + + @param nDecPlaces + The number of decimals to be generated. Effectively fValue is rounded at + this position, specifying nDecPlaces <= 0 accordingly rounds the value + before the decimal point and fills with zeros. + If eFormat == rtl_math_StringFormat_Automatic and nDecPlaces == + rtl_math_DecimalPlaces_Max, the highest number of significant decimals + possible is generated. + If eFormat == rtl_math_StringFormat_G, nDecPlaces specifies the number of + significant digits instead. If nDecPlaces == + rtl_math_DecimalPlaces_DefaultSignificance, the default number (currently 6 + as implemented by most libraries) of significant digits is generated. + According to the ANSI C90 standard the E style will be used only if the + exponent resulting from the conversion is less than -4 or greater than or + equal to the precision. However, as opposed to the ANSI standard, trailing + zeros are not necessarily removed from the fractional portion of the result + unless bEraseTrailingDecZeros == true was specified. + + @param cDecSeparator + The decimal separator. + + @param pGroups + Either null (no grouping is used), or a null-terminated list of group + lengths. Each group length must be strictly positive. If the number of + digits in a conversion exceeds the specified range, the last (highest) group + length is repeated as needed. Values are applied from right to left, for a + grouping of 1,00,00,000 you'd have to specify pGroups={3,2,0}. + + @param cGroupSeparator + The group separator. Ignored if pGroups is null. + + @param bEraseTrailingDecZeros + Trailing zeros in decimal places are erased. + */ +SAL_DLLPUBLIC void SAL_CALL rtl_math_doubleToString(rtl_String ** pResult, + sal_Int32 * pResultCapacity, + sal_Int32 nResultOffset, double fValue, + enum rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + char cDecSeparator, + sal_Int32 const * pGroups, + char cGroupSeparator, + sal_Bool bEraseTrailingDecZeros) + SAL_THROW_EXTERN_C(); + +/** Conversions analogous to sprintf() using internal rounding. + + +/-HUGE_VAL are converted to "INF" and "-INF", NAN values are + converted to "NaN". + + @param pResult + Returns the resulting Unicode string. Must itself not be null, and must + point to either null or a valid string. + + @param pResultCapacity + If null, pResult is considered to point to immutable strings, and a new + string will be allocated in pResult. + If non-null, it points to the current capacity of pResult, which is + considered to point to a string buffer (pResult must not itself be null in + this case, and must point to a string that has room for the given capacity). + The string representation of the given double value is inserted into pResult + at position nResultOffset. If pResult's current capacity is too small, a + new string buffer will be allocated in pResult as necessary, and + pResultCapacity will contain the new capacity on return. + + @param nResultOffset + If pResult is used as a string buffer (i.e., pResultCapacity is non-null), + nResultOffset specifies the insertion offset within the buffer. Ignored + otherwise. + + @param fValue + The value to convert. + + @param eFormat + The format to use, one of rtl_math_StringFormat. + + @param nDecPlaces + The number of decimals to be generated. Effectively fValue is rounded at + this position, specifying nDecPlaces <= 0 accordingly rounds the value + before the decimal point and fills with zeros. + If eFormat == rtl_math_StringFormat_Automatic and nDecPlaces == + rtl_math_DecimalPlaces_Max, the highest number of significant decimals + possible is generated. + If eFormat == rtl_math_StringFormat_G, nDecPlaces specifies the number of + significant digits instead. If nDecPlaces == + rtl_math_DecimalPlaces_DefaultSignificance, the default number (currently 6 + as implemented by most libraries) of significant digits is generated. + According to the ANSI C90 standard the E style will be used only if the + exponent resulting from the conversion is less than -4 or greater than or + equal to the precision. However, as opposed to the ANSI standard, trailing + zeros are not necessarily removed from the fractional portion of the result + unless bEraseTrailingDecZeros == true was specified. + + @param cDecSeparator + The decimal separator. + + @param pGroups + Either null (no grouping is used), or a null-terminated list of group + lengths. Each group length must be strictly positive. If the number of + digits in a conversion exceeds the specified range, the last (highest) group + length is repeated as needed. Values are applied from right to left, for a + grouping of 1,00,00,000 you'd have to specify pGroups={3,2,0}. + + @param cGroupSeparator + The group separator. Ignored if pGroups is null. + + @param bEraseTrailingDecZeros + Trailing zeros in decimal places are erased. + */ +SAL_DLLPUBLIC void SAL_CALL rtl_math_doubleToUString(rtl_uString ** pResult, + sal_Int32 * pResultCapacity, + sal_Int32 nResultOffset, double fValue, + enum rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + sal_Unicode cDecSeparator, + sal_Int32 const * pGroups, + sal_Unicode cGroupSeparator, + sal_Bool bEraseTrailingDecZeros) + SAL_THROW_EXTERN_C(); + +/** Conversion analogous to strtod(), convert a string representing a + decimal number into a double value. + + Leading tabs (0x09) and spaces (0x20) are eaten. Overflow returns + +/-HUGE_VAL, underflow 0. In both cases pStatus is set to + rtl_math_ConversionStatus_OutOfRange, otherwise to + rtl_math_ConversionStatus_Ok. "INF", "-INF" and "+/-1.#INF" are + recognized as +/-HUGE_VAL, pStatus is set to + rtl_math_ConversionStatus_OutOfRange. "NaN" and "+/-1.#NAN" are + recognized and the value is set to +/-NAN, pStatus is set to + rtl_math_ConversionStatus_Ok. + + @param pBegin + Points to the start of the byte string to convert. Must not be null. + + @param pEnd + Points one past the end of the byte string to convert. The condition + pEnd >= pBegin must hold. + + @param cDecSeparator + The decimal separator. + + @param cGroupSeparator + The group (aka thousands) separator. + + @param pStatus + If non-null, returns the status of the conversion. + + @param pParsedEnd + If non-null, returns one past the position of the last character parsed + away. Thus if [pBegin..pEnd) only contains the numerical string to be + parsed, *pParsedEnd == pEnd on return. If no numerical (sub-)string is + found, *pParsedEnd == pBegin on return, even if there was leading + whitespace. + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_stringToDouble( + char const * pBegin, char const * pEnd, char cDecSeparator, + char cGroupSeparator, enum rtl_math_ConversionStatus * pStatus, + char const ** pParsedEnd) SAL_THROW_EXTERN_C(); + +/** Conversion analogous to strtod(), convert a string representing a + decimal number into a double value. + + Leading tabs (U+0009) and spaces (U+0020) are eaten. Overflow returns + +/-HUGE_VAL, underflow 0. In both cases pStatus is set to + rtl_math_ConversionStatus_OutOfRange, otherwise to + rtl_math_ConversionStatus_Ok. "INF", "-INF" and "+/-1.#INF" are + recognized as +/-HUGE_VAL, pStatus is set to + rtl_math_ConversionStatus_OutOfRange. "NaN" and "+/-1.#NAN" are + recognized and the value is set to +/-NAN, pStatus is set to + rtl_math_ConversionStatus_Ok. + + @param pBegin + Points to the start of the Unicode string to convert. Must not be null. + + @param pEnd + Points one past the end of the Unicode string to convert. The condition + pEnd >= pBegin must hold. + + @param cDecSeparator + The decimal separator. + + @param cGroupSeparator + The group (aka thousands) separator. + + @param pStatus + If non-null, returns the status of the conversion. + + @param pParsedEnd + If non-null, returns one past the position of the last character parsed + away. Thus if [pBegin..pEnd) only contains the numerical string to be + parsed, *pParsedEnd == pEnd on return. If no numerical (sub-)string is + found, *pParsedEnd == pBegin on return, even if there was leading + whitespace. + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_uStringToDouble( + sal_Unicode const * pBegin, sal_Unicode const * pEnd, + sal_Unicode cDecSeparator, sal_Unicode cGroupSeparator, + enum rtl_math_ConversionStatus * pStatus, sal_Unicode const ** pParsedEnd) + SAL_THROW_EXTERN_C(); + +/** Rounds a double value. + + @param fValue + Specifies the value to be rounded. + + @param nDecPlaces + Specifies the decimal place where rounding occurs. Must be in the range + -20 to +20, inclusive. Negative if rounding occurs before the decimal + point. + + @param eMode + Specifies the rounding mode. + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_round(double fValue, int nDecPlaces, + enum rtl_math_RoundingMode eMode) + SAL_THROW_EXTERN_C(); + +/** Scales fVal to a power of 10 without calling pow() or div() for nExp values + between -16 and +16, providing a faster method. + + @param fValue + The value to be raised. + + @param nExp + The exponent. + + @return + fVal * pow(10.0, nExp) + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_pow10Exp(double fValue, int nExp) SAL_THROW_EXTERN_C(); + +/** Rounds value to 15 significant decimal digits. + + @param fValue + The value to be rounded. + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C(); + +/** Test equality of two values with an accuracy of the magnitude of the + given values scaled by 2^-48 (4 bits roundoff stripped). + + @attention + approxEqual( value!=0.0, 0.0 ) _never_ yields true. + + @since LibreOffice 5.3 + */ +SAL_DLLPUBLIC bool SAL_CALL rtl_math_approxEqual(double a, double b) SAL_THROW_EXTERN_C(); + +/** Returns more accurate e^x-1 for x near 0 than calculating directly. + + expm1 is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term e^x-1. + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_expm1(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns more accurate log(1+x) for x near 0 than calculating directly. + + log1p is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term log(1+x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_log1p(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns more accurate atanh(x) for x near 0 than calculating + 0.5*log((1+x)/(1-x)). + + atanh is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term atanh(x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_atanh(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns values of the Errorfunction erf. + + erf is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term erf(x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_erf(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns values of the complement Errorfunction erfc. + + erfc is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term erfc(x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns values of the inverse hyperbolic sine. + + asinh is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term asinh(x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C(); + +/** Returns values of the inverse hyperbolic cosine. + + acosh is part of the C99 standard, but not provided by some compilers. + + @param fValue + The value x in the term acosh(x). + */ +SAL_DLLPUBLIC double SAL_CALL rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C(); + +#if defined __cplusplus +} +#endif /* __cplusplus */ + +#endif /* INCLUDED_RTL_MATH_H */ + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ diff --git a/include/rtl/math.hxx b/include/rtl/math.hxx new file mode 100644 index 0000000000..661ddf1d13 --- /dev/null +++ b/include/rtl/math.hxx @@ -0,0 +1,511 @@ +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ +/* + * This file is part of the LibreOffice project. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + * + * This file incorporates work covered by the following license notice: + * + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed + * with this work for additional information regarding copyright + * ownership. The ASF licenses this file to you under the Apache + * License, Version 2.0 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.apache.org/licenses/LICENSE-2.0 . + */ + +/* + * This file is part of LibreOffice published API. + */ + +#ifndef INCLUDED_RTL_MATH_HXX +#define INCLUDED_RTL_MATH_HXX + +#include "rtl/math.h" +#include "rtl/strbuf.hxx" +#include "rtl/string.hxx" +#include "rtl/ustring.hxx" +#include "rtl/ustrbuf.hxx" +#include "sal/mathconf.h" +#include "sal/types.h" + +#include <cstddef> +#include <math.h> + +namespace rtl { + +namespace math { + +/** A wrapper around rtl_math_doubleToString. + */ +inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + char cDecSeparator, + sal_Int32 const * pGroups, + char cGroupSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl::OString aResult; + rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces, + cDecSeparator, pGroups, cGroupSeparator, + bEraseTrailingDecZeros); + return aResult; +} + +/** A wrapper around rtl_math_doubleToString, with no grouping. + */ +inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + char cDecSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl::OString aResult; + rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces, + cDecSeparator, NULL, 0, bEraseTrailingDecZeros); + return aResult; +} + +/** A wrapper around rtl_math_doubleToString that appends to an + rtl::OStringBuffer. + + @since LibreOffice 5.4 +*/ +inline void doubleToStringBuffer( + rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, char cDecSeparator, sal_Int32 const * pGroups, + char cGroupSeparator, bool bEraseTrailingDecZeros = false) +{ + rtl_String ** pData; + sal_Int32 * pCapacity; + rBuffer.accessInternals(&pData, &pCapacity); + rtl_math_doubleToString( + pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces, + cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros); +} + +/** A wrapper around rtl_math_doubleToString that appends to an + rtl::OStringBuffer, with no grouping. + + @since LibreOffice 5.4 +*/ +inline void doubleToStringBuffer( + rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, char cDecSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl_String ** pData; + sal_Int32 * pCapacity; + rBuffer.accessInternals(&pData, &pCapacity); + rtl_math_doubleToString( + pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces, + cDecSeparator, NULL, 0, bEraseTrailingDecZeros); +} + +/** A wrapper around rtl_math_doubleToUString. + */ +inline rtl::OUString doubleToUString(double fValue, + rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + sal_Unicode cDecSeparator, + sal_Int32 const * pGroups, + sal_Unicode cGroupSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl::OUString aResult; + rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces, + cDecSeparator, pGroups, cGroupSeparator, + bEraseTrailingDecZeros); + return aResult; +} + +/** A wrapper around rtl_math_doubleToUString, with no grouping. + */ +inline rtl::OUString doubleToUString(double fValue, + rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + sal_Unicode cDecSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl::OUString aResult; + rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces, + cDecSeparator, NULL, 0, bEraseTrailingDecZeros); + return aResult; +} + +/** A wrapper around rtl_math_doubleToUString that appends to an + rtl::OUStringBuffer. + */ +inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue, + rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + sal_Unicode cDecSeparator, + sal_Int32 const * pGroups, + sal_Unicode cGroupSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl_uString ** pData; + sal_Int32 * pCapacity; + rBuffer.accessInternals( &pData, &pCapacity ); + rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue, + eFormat, nDecPlaces, cDecSeparator, pGroups, + cGroupSeparator, bEraseTrailingDecZeros); +} + +/** A wrapper around rtl_math_doubleToUString that appends to an + rtl::OUStringBuffer, with no grouping. + */ +inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue, + rtl_math_StringFormat eFormat, + sal_Int32 nDecPlaces, + sal_Unicode cDecSeparator, + bool bEraseTrailingDecZeros = false) +{ + rtl_uString ** pData; + sal_Int32 * pCapacity; + rBuffer.accessInternals( &pData, &pCapacity ); + rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue, + eFormat, nDecPlaces, cDecSeparator, NULL, 0, + bEraseTrailingDecZeros); +} + +/** A wrapper around rtl_math_stringToDouble. + */ +#ifdef LIBO_INTERNAL_ONLY +inline double stringToDouble(std::string_view aString, + char cDecSeparator, char cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + char const * pBegin = aString.data(); + char const * pEnd; + double fResult = rtl_math_stringToDouble(pBegin, + pBegin + aString.size(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#else +inline double stringToDouble(rtl::OString const & rString, + char cDecSeparator, char cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + char const * pBegin = rString.getStr(); + char const * pEnd; + double fResult = rtl_math_stringToDouble(pBegin, + pBegin + rString.getLength(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#endif + + +/** A wrapper around rtl_math_uStringToDouble. + */ +#ifdef LIBO_INTERNAL_ONLY +inline double stringToDouble(std::u16string_view aString, + sal_Unicode cDecSeparator, + sal_Unicode cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + sal_Unicode const * pBegin = aString.data(); + sal_Unicode const * pEnd; + double fResult = rtl_math_uStringToDouble(pBegin, + pBegin + aString.size(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#else +inline double stringToDouble(rtl::OUString const & rString, + sal_Unicode cDecSeparator, + sal_Unicode cGroupSeparator, + rtl_math_ConversionStatus * pStatus = NULL, + sal_Int32 * pParsedEnd = NULL) +{ + sal_Unicode const * pBegin = rString.getStr(); + sal_Unicode const * pEnd; + double fResult = rtl_math_uStringToDouble(pBegin, + pBegin + rString.getLength(), + cDecSeparator, cGroupSeparator, + pStatus, &pEnd); + if (pParsedEnd != NULL) + *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin); + return fResult; +} +#endif + +/** A wrapper around rtl_math_round. + */ +inline double round( + double fValue, int nDecPlaces = 0, + rtl_math_RoundingMode eMode = rtl_math_RoundingMode_Corrected) +{ + return rtl_math_round(fValue, nDecPlaces, eMode); +} + +/** A wrapper around rtl_math_pow10Exp. + */ +inline double pow10Exp(double fValue, int nExp) +{ + return rtl_math_pow10Exp(fValue, nExp); +} + +/** A wrapper around rtl_math_approxValue. + */ +inline double approxValue(double fValue) +{ + return rtl_math_approxValue(fValue); +} + +/** A wrapper around rtl_math_expm1. + */ +inline double expm1(double fValue) +{ + return rtl_math_expm1(fValue); +} + +/** A wrapper around rtl_math_log1p. + */ +inline double log1p(double fValue) +{ + return rtl_math_log1p(fValue); +} + +/** A wrapper around rtl_math_atanh. + */ +inline double atanh(double fValue) +{ + return rtl_math_atanh(fValue); +} + +/** A wrapper around rtl_math_erf. + */ +inline double erf(double fValue) +{ + return rtl_math_erf(fValue); +} + +/** A wrapper around rtl_math_erfc. + */ +inline double erfc(double fValue) +{ + return rtl_math_erfc(fValue); +} + +/** A wrapper around rtl_math_asinh. + */ +inline double asinh(double fValue) +{ + return rtl_math_asinh(fValue); +} + +/** A wrapper around rtl_math_acosh. + */ +inline double acosh(double fValue) +{ + return rtl_math_acosh(fValue); +} + +/** A wrapper around rtl_math_approxEqual. + */ +inline bool approxEqual(double a, double b) +{ + return rtl_math_approxEqual( a, b ); +} + +/** Test equality of two values with an accuracy defined by nPrec + + @attention + approxEqual( value!=0.0, 0.0 ) _never_ yields true. + */ +inline bool approxEqual(double a, double b, sal_Int16 nPrec) +{ + if ( a == b ) + return true; + double x = a - b; + return (x < 0.0 ? -x : x) + < ((a < 0.0 ? -a : a) * (1.0 / (pow(2.0, nPrec)))); +} + +/** Add two values. + + If signs differ and the absolute values are equal according to approxEqual() + the method returns 0.0 instead of calculating the sum. + + If you wanted to sum up multiple values it would be convenient not to call + approxAdd() for each value but instead remember the first value not equal to + 0.0, add all other values using normal + operator, and with the result and + the remembered value call approxAdd(). + */ +inline double approxAdd(double a, double b) +{ + if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0)) + && approxEqual( a, -b ) ) + return 0.0; + return a + b; +} + +/** Subtract two values (a-b). + + If signs are identical and the values are equal according to approxEqual() + the method returns 0.0 instead of calculating the subtraction. + */ +inline double approxSub(double a, double b) +{ + if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) ) + return 0.0; + return a - b; +} + +/** floor() method taking approxValue() into account. + + Use for expected integer values being calculated by double functions. + */ +inline double approxFloor(double a) +{ + return floor( approxValue( a )); +} + +/** ceil() method taking approxValue() into account. + + Use for expected integer values being calculated by double functions. + */ +inline double approxCeil(double a) +{ + return ceil( approxValue( a )); +} + +/** Tests whether a value is neither INF nor NAN. + */ +inline bool isFinite(double d) +{ + return SAL_MATH_FINITE(d); +} + +/** If a value represents +INF or -INF. + + The sign bit may be queried with isSignBitSet(). + + If isFinite(d)==false and isInf(d)==false then NAN. + */ +inline bool isInf(double d) +{ + // exponent==0x7ff fraction==0 + return !SAL_MATH_FINITE(d) && + (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0) + && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo + == 0); +} + +/** Test on any QNAN or SNAN. + */ +inline bool isNan(double d) +{ + // exponent==0x7ff fraction!=0 + return !SAL_MATH_FINITE(d) && ( + (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0) + || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo + != 0) ); +} + +/** If the sign bit is set. + */ +inline bool isSignBitSet(double d) +{ + return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0; +} + +/** Set to +INF if bNegative==false or -INF if bNegative==true. + */ +inline void setInf(double * pd, bool bNegative) +{ + union + { + double sd; + sal_math_Double md; + }; + md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000; + md.w32_parts.lsw = 0; + *pd = sd; +} + +/** Set a QNAN. + */ +inline void setNan(double * pd) +{ + union + { + double sd; + sal_math_Double md; + }; + md.w32_parts.msw = 0x7FFFFFFF; + md.w32_parts.lsw = 0xFFFFFFFF; + *pd = sd; +} + +/** If a value is a valid argument for sin(), cos(), tan(). + + IEEE 754 specifies that absolute values up to 2^64 (=1.844e19) for the + radian must be supported by trigonometric functions. Unfortunately, at + least on x86 architectures, the FPU doesn't generate an error pattern for + values >2^64 but produces erroneous results instead and sets only the + "invalid operation" (IM) flag in the status word :-( Thus the application + has to handle it itself. + */ +inline bool isValidArcArg(double d) +{ + return fabs(d) + <= (static_cast< double >(static_cast< unsigned long >(0x80000000)) + * static_cast< double >(static_cast< unsigned long >(0x80000000)) + * 4); +} + +/** Safe sin(), returns NAN if not valid. + */ +inline double sin(double d) +{ + if ( isValidArcArg( d ) ) + return ::sin( d ); + setNan( &d ); + return d; +} + +/** Safe cos(), returns NAN if not valid. + */ +inline double cos(double d) +{ + if ( isValidArcArg( d ) ) + return ::cos( d ); + setNan( &d ); + return d; +} + +/** Safe tan(), returns NAN if not valid. + */ +inline double tan(double d) +{ + if ( isValidArcArg( d ) ) + return ::tan( d ); + setNan( &d ); + return d; +} + +} + +} + +#endif // INCLUDED_RTL_MATH_HXX + +/* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |