LibreOffice
LibreOffice 6.0 SDK C/C++ API Reference
math.hxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
11  * Licensed to the Apache Software Foundation (ASF) under one or more
12  * contributor license agreements. See the NOTICE file distributed
13  * with this work for additional information regarding copyright
14  * ownership. The ASF licenses this file to you under the Apache
15  * License, Version 2.0 (the "License"); you may not use this file
16  * except in compliance with the License. You may obtain a copy of
17  * the License at http://www.apache.org/licenses/LICENSE-2.0 .
18  */
19 
20 #ifndef INCLUDED_RTL_MATH_HXX
21 #define INCLUDED_RTL_MATH_HXX
22 
23 #include <rtl/math.h>
24 #include <rtl/strbuf.hxx>
25 #include <rtl/string.hxx>
26 #include <rtl/ustring.hxx>
27 #include <rtl/ustrbuf.hxx>
28 #include <sal/mathconf.h>
29 #include <sal/types.h>
30 
31 #include <cstddef>
32 #include <math.h>
33 
34 namespace rtl {
35 
36 namespace math {
37 
40 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
41  sal_Int32 nDecPlaces,
42  sal_Char cDecSeparator,
43  sal_Int32 const * pGroups,
44  sal_Char cGroupSeparator,
45  bool bEraseTrailingDecZeros = false)
46 {
47  rtl::OString aResult;
48  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
49  cDecSeparator, pGroups, cGroupSeparator,
50  bEraseTrailingDecZeros);
51  return aResult;
52 }
53 
56 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
57  sal_Int32 nDecPlaces,
58  sal_Char cDecSeparator,
59  bool bEraseTrailingDecZeros = false)
60 {
61  rtl::OString aResult;
62  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
63  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
64  return aResult;
65 }
66 
73  rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
74  sal_Int32 nDecPlaces, sal_Char cDecSeparator, sal_Int32 const * pGroups,
75  sal_Char cGroupSeparator, bool bEraseTrailingDecZeros = false)
76 {
77  rtl_String ** pData;
78  sal_Int32 * pCapacity;
79  rBuffer.accessInternals(&pData, &pCapacity);
81  pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
82  cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
83 }
84 
91  rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
92  sal_Int32 nDecPlaces, sal_Char cDecSeparator,
93  bool bEraseTrailingDecZeros = false)
94 {
95  rtl_String ** pData;
96  sal_Int32 * pCapacity;
97  rBuffer.accessInternals(&pData, &pCapacity);
99  pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
100  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
101 }
102 
105 inline rtl::OUString doubleToUString(double fValue,
106  rtl_math_StringFormat eFormat,
107  sal_Int32 nDecPlaces,
108  sal_Unicode cDecSeparator,
109  sal_Int32 const * pGroups,
110  sal_Unicode cGroupSeparator,
111  bool bEraseTrailingDecZeros = false)
112 {
113  rtl::OUString aResult;
114  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
115  cDecSeparator, pGroups, cGroupSeparator,
116  bEraseTrailingDecZeros);
117  return aResult;
118 }
119 
122 inline rtl::OUString doubleToUString(double fValue,
123  rtl_math_StringFormat eFormat,
124  sal_Int32 nDecPlaces,
125  sal_Unicode cDecSeparator,
126  bool bEraseTrailingDecZeros = false)
127 {
128  rtl::OUString aResult;
129  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
130  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
131  return aResult;
132 }
133 
137 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
138  rtl_math_StringFormat eFormat,
139  sal_Int32 nDecPlaces,
140  sal_Unicode cDecSeparator,
141  sal_Int32 const * pGroups,
142  sal_Unicode cGroupSeparator,
143  bool bEraseTrailingDecZeros = false)
144 {
145  rtl_uString ** pData;
146  sal_Int32 * pCapacity;
147  rBuffer.accessInternals( &pData, &pCapacity );
148  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
149  eFormat, nDecPlaces, cDecSeparator, pGroups,
150  cGroupSeparator, bEraseTrailingDecZeros);
151 }
152 
156 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
157  rtl_math_StringFormat eFormat,
158  sal_Int32 nDecPlaces,
159  sal_Unicode cDecSeparator,
160  bool bEraseTrailingDecZeros = false)
161 {
162  rtl_uString ** pData;
163  sal_Int32 * pCapacity;
164  rBuffer.accessInternals( &pData, &pCapacity );
165  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
166  eFormat, nDecPlaces, cDecSeparator, NULL, 0,
167  bEraseTrailingDecZeros);
168 }
169 
172 inline double stringToDouble(rtl::OString const & rString,
173  sal_Char cDecSeparator, sal_Char cGroupSeparator,
174  rtl_math_ConversionStatus * pStatus = NULL,
175  sal_Int32 * pParsedEnd = NULL)
176 {
177  sal_Char const * pBegin = rString.getStr();
178  sal_Char const * pEnd;
179  double fResult = rtl_math_stringToDouble(pBegin,
180  pBegin + rString.getLength(),
181  cDecSeparator, cGroupSeparator,
182  pStatus, &pEnd);
183  if (pParsedEnd != NULL)
184  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
185  return fResult;
186 }
187 
190 inline double stringToDouble(rtl::OUString const & rString,
191  sal_Unicode cDecSeparator,
192  sal_Unicode cGroupSeparator,
193  rtl_math_ConversionStatus * pStatus = NULL,
194  sal_Int32 * pParsedEnd = NULL)
195 {
196  sal_Unicode const * pBegin = rString.getStr();
197  sal_Unicode const * pEnd;
198  double fResult = rtl_math_uStringToDouble(pBegin,
199  pBegin + rString.getLength(),
200  cDecSeparator, cGroupSeparator,
201  pStatus, &pEnd);
202  if (pParsedEnd != NULL)
203  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
204  return fResult;
205 }
206 
209 inline double round(
210  double fValue, int nDecPlaces = 0,
212 {
213  return rtl_math_round(fValue, nDecPlaces, eMode);
214 }
215 
218 inline double pow10Exp(double fValue, int nExp)
219 {
220  return rtl_math_pow10Exp(fValue, nExp);
221 }
222 
225 inline double approxValue(double fValue)
226 {
227  return rtl_math_approxValue(fValue);
228 }
229 
232 inline double expm1(double fValue)
233 {
234  return rtl_math_expm1(fValue);
235 }
236 
239 inline double log1p(double fValue)
240 {
241  return rtl_math_log1p(fValue);
242 }
243 
246 inline double atanh(double fValue)
247 {
248  return rtl_math_atanh(fValue);
249 }
250 
253 inline double erf(double fValue)
254 {
255  return rtl_math_erf(fValue);
256 }
257 
260 inline double erfc(double fValue)
261 {
262  return rtl_math_erfc(fValue);
263 }
264 
267 inline double asinh(double fValue)
268 {
269  return rtl_math_asinh(fValue);
270 }
271 
274 inline double acosh(double fValue)
275 {
276  return rtl_math_acosh(fValue);
277 }
278 
281 inline bool approxEqual(double a, double b)
282 {
283  return rtl_math_approxEqual( a, b );
284 }
285 
291 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
292 {
293  if ( a == b )
294  return true;
295  double x = a - b;
296  return (x < 0.0 ? -x : x)
297  < ((a < 0.0 ? -a : a) * (1.0 / (pow(2.0, nPrec))));
298 }
299 
310 inline double approxAdd(double a, double b)
311 {
312  if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
313  && approxEqual( a, -b ) )
314  return 0.0;
315  return a + b;
316 }
317 
323 inline double approxSub(double a, double b)
324 {
325  if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
326  return 0.0;
327  return a - b;
328 }
329 
334 inline double approxFloor(double a)
335 {
336  return floor( approxValue( a ));
337 }
338 
343 inline double approxCeil(double a)
344 {
345  return ceil( approxValue( a ));
346 }
347 
350 inline bool isFinite(double d)
351 {
352  return SAL_MATH_FINITE(d);
353 }
354 
361 inline bool isInf(double d)
362 {
363  // exponent==0x7ff fraction==0
364  return !SAL_MATH_FINITE(d) &&
365  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
366  && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
367  == 0);
368 }
369 
372 inline bool isNan(double d)
373 {
374  // exponent==0x7ff fraction!=0
375  return !SAL_MATH_FINITE(d) && (
376  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
377  || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
378  != 0) );
379 }
380 
383 inline bool isSignBitSet(double d)
384 {
385  return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
386 }
387 
390 inline void setInf(double * pd, bool bNegative)
391 {
392  union
393  {
394  double sd;
395  sal_math_Double md;
396  };
397  md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
398  md.w32_parts.lsw = 0;
399  *pd = sd;
400 }
401 
404 inline void setNan(double * pd)
405 {
406  union
407  {
408  double sd;
409  sal_math_Double md;
410  };
411  md.w32_parts.msw = 0x7FFFFFFF;
412  md.w32_parts.lsw = 0xFFFFFFFF;
413  *pd = sd;
414 }
415 
425 inline bool isValidArcArg(double d)
426 {
427  return fabs(d)
428  <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
429  * static_cast< double >(static_cast< unsigned long >(0x80000000))
430  * 2);
431 }
432 
435 inline double sin(double d)
436 {
437  if ( isValidArcArg( d ) )
438  return ::sin( d );
439  setNan( &d );
440  return d;
441 }
442 
445 inline double cos(double d)
446 {
447  if ( isValidArcArg( d ) )
448  return ::cos( d );
449  setNan( &d );
450  return d;
451 }
452 
455 inline double tan(double d)
456 {
457  if ( isValidArcArg( d ) )
458  return ::tan( d );
459  setNan( &d );
460  return d;
461 }
462 
463 }
464 
465 }
466 
467 #endif // INCLUDED_RTL_MATH_HXX
468 
469 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool approxEqual(double a, double b)
A wrapper around rtl_math_approxEqual.
Definition: math.hxx:281
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:323
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:361
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:246
double stringToDouble(rtl::OString const &rString, sal_Char cDecSeparator, sal_Char cGroupSeparator, rtl_math_ConversionStatus *pStatus=NULL, sal_Int32 *pParsedEnd=NULL)
A wrapper around rtl_math_stringToDouble.
Definition: math.hxx:172
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:633
SAL_DLLPUBLIC bool rtl_math_approxEqual(double a, double b) SAL_THROW_EXTERN_C()
Test equality of two values with an accuracy of the magnitude of the given values scaled by 2^-48 (4 ...
char sal_Char
A legacy synonym for char.
Definition: types.h:121
SAL_DLLPUBLIC double rtl_math_erf(double fValue) SAL_THROW_EXTERN_C()
Returns values of the Errorfunction erf.
double cos(double d)
Safe cos(), returns NAN if not valid.
Definition: math.hxx:445
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:260
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:355
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:390
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:334
double round(double fValue, int nDecPlaces=0, rtl_math_RoundingMode eMode=rtl_math_RoundingMode_Corrected)
A wrapper around rtl_math_round.
Definition: math.hxx:209
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:218
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:310
SAL_DLLPUBLIC double rtl_math_atanh(double fValue) SAL_THROW_EXTERN_C()
Returns more accurate atanh(x) for x near 0 than calculating 0.5*log((1+x)/(1-x)).
const sal_Char * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the characters of this string.
Definition: string.hxx:428
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:350
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1279
SAL_DLLPUBLIC double rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C()
Returns values of the complement Errorfunction erfc.
double asinh(double fValue)
A wrapper around rtl_math_asinh.
Definition: math.hxx:267
SAL_DLLPUBLIC double rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C()
Rounds value to 15 significant decimal digits.
double sin(double d)
Safe sin(), returns NAN if not valid.
Definition: math.hxx:435
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)
A wrapper around rtl_math_doubleToUString.
Definition: math.hxx:105
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:455
SAL_DLLPUBLIC double rtl_math_expm1(double fValue) SAL_THROW_EXTERN_C()
Returns more accurate e^x-1 for x near 0 than calculating directly.
SAL_DLLPUBLIC double rtl_math_log1p(double fValue) SAL_THROW_EXTERN_C()
Returns more accurate log(1+x) for x near 0 than calculating directly.
double approxValue(double fValue)
A wrapper around rtl_math_approxValue.
Definition: math.hxx:225
rtl_math_StringFormat
Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString and rtl_math_doubleToUStrin...
Definition: math.h:36
bool isSignBitSet(double d)
If the sign bit is set.
Definition: math.hxx:383
rtl_math_ConversionStatus
Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
Definition: math.h:83
SAL_DLLPUBLIC double rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic cosine.
rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat, sal_Int32 nDecPlaces, sal_Char cDecSeparator, sal_Int32 const *pGroups, sal_Char cGroupSeparator, bool bEraseTrailingDecZeros=false)
A wrapper around rtl_math_doubleToString.
Definition: math.hxx:40
sal_uInt16 sal_Unicode
Definition: types.h:142
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:655
SAL_DLLPUBLIC double 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()
Conversion analogous to strtod(), convert a string representing a decimal number into a double value...
bool isValidArcArg(double d)
If a value is a valid argument for sin(), cos(), tan().
Definition: math.hxx:425
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:232
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:66
SAL_DLLPUBLIC double rtl_math_pow10Exp(double fValue, int nExp) SAL_THROW_EXTERN_C()
Scales fVal to a power of 10 without calling pow() or div() for nExp values between -16 and +16...
double acosh(double fValue)
A wrapper around rtl_math_acosh.
Definition: math.hxx:274
SAL_DLLPUBLIC double rtl_math_stringToDouble(sal_Char const *pBegin, sal_Char const *pEnd, sal_Char cDecSeparator, sal_Char cGroupSeparator, enum rtl_math_ConversionStatus *pStatus, sal_Char const **pParsedEnd) SAL_THROW_EXTERN_C()
Conversion analogous to strtod(), convert a string representing a decimal number into a double value...
SAL_DLLPUBLIC void rtl_math_doubleToString(rtl_String **pResult, sal_Int32 *pResultCapacity, sal_Int32 nResultOffset, double fValue, enum rtl_math_StringFormat eFormat, sal_Int32 nDecPlaces, sal_Char cDecSeparator, sal_Int32 const *pGroups, sal_Char cGroupSeparator, sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C()
Conversions analogous to sprintf() using internal rounding.
void setNan(double *pd)
Set a QNAN.
Definition: math.hxx:404
SAL_DLLPUBLIC double rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic sine.
void doubleToStringBuffer(rtl::OStringBuffer &rBuffer, double fValue, rtl_math_StringFormat eFormat, sal_Int32 nDecPlaces, sal_Char cDecSeparator, sal_Int32 const *pGroups, sal_Char cGroupSeparator, bool bEraseTrailingDecZeros=false)
A wrapper around rtl_math_doubleToString that appends to an rtl::OStringBuffer.
Definition: math.hxx:72
SAL_DLLPUBLIC double rtl_math_round(double fValue, int nDecPlaces, enum rtl_math_RoundingMode eMode) SAL_THROW_EXTERN_C()
Rounds a double value.
Definition: bootstrap.hxx:29
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:120
void accessInternals(rtl_String ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OStringBuffer, for effective manipulation.
Definition: strbuf.hxx:1008
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:402
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:253
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:372
Like HalfUp, but corrects roundoff errors, preferred.
Definition: math.h:104
SAL_DLLPUBLIC void 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()
Conversions analogous to sprintf() using internal rounding.
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)
A wrapper around rtl_math_doubleToUString that appends to an rtl::OUStringBuffer. ...
Definition: math.hxx:137
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:60
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:239
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:322
rtl_math_RoundingMode
Rounding modes for rtl_math_round.
Definition: math.h:100
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:94
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:343