AlterOffice
AlterOffice 3.4 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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 
4 #ifndef INCLUDED_RTL_MATH_HXX
5 #define INCLUDED_RTL_MATH_HXX
6 
7 #include "rtl/math.h"
8 #include "rtl/strbuf.hxx"
9 #include "rtl/string.hxx"
10 #include "rtl/ustring.hxx"
11 #include "rtl/ustrbuf.hxx"
12 #include "sal/mathconf.h"
13 #include "sal/types.h"
14 
15 #include <cstddef>
16 #include <math.h>
17 
18 namespace rtl {
19 
20 namespace math {
21 
24 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
25  sal_Int32 nDecPlaces,
26  char cDecSeparator,
27  sal_Int32 const * pGroups,
28  char cGroupSeparator,
29  bool bEraseTrailingDecZeros = false)
30 {
31  rtl::OString aResult;
32  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
33  cDecSeparator, pGroups, cGroupSeparator,
34  bEraseTrailingDecZeros);
35  return aResult;
36 }
37 
40 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
41  sal_Int32 nDecPlaces,
42  char cDecSeparator,
43  bool bEraseTrailingDecZeros = false)
44 {
45  rtl::OString aResult;
46  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
47  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
48  return aResult;
49 }
50 
57  rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
58  sal_Int32 nDecPlaces, char cDecSeparator, sal_Int32 const * pGroups,
59  char cGroupSeparator, bool bEraseTrailingDecZeros = false)
60 {
61  rtl_String ** pData;
62  sal_Int32 * pCapacity;
63  rBuffer.accessInternals(&pData, &pCapacity);
65  pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
66  cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
67 }
68 
75  rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
76  sal_Int32 nDecPlaces, char cDecSeparator,
77  bool bEraseTrailingDecZeros = false)
78 {
79  rtl_String ** pData;
80  sal_Int32 * pCapacity;
81  rBuffer.accessInternals(&pData, &pCapacity);
83  pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
84  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
85 }
86 
89 inline rtl::OUString doubleToUString(double fValue,
90  rtl_math_StringFormat eFormat,
91  sal_Int32 nDecPlaces,
92  sal_Unicode cDecSeparator,
93  sal_Int32 const * pGroups,
94  sal_Unicode cGroupSeparator,
95  bool bEraseTrailingDecZeros = false)
96 {
97  rtl::OUString aResult;
98  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
99  cDecSeparator, pGroups, cGroupSeparator,
100  bEraseTrailingDecZeros);
101  return aResult;
102 }
103 
106 inline rtl::OUString doubleToUString(double fValue,
107  rtl_math_StringFormat eFormat,
108  sal_Int32 nDecPlaces,
109  sal_Unicode cDecSeparator,
110  bool bEraseTrailingDecZeros = false)
111 {
112  rtl::OUString aResult;
113  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
114  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
115  return aResult;
116 }
117 
121 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
122  rtl_math_StringFormat eFormat,
123  sal_Int32 nDecPlaces,
124  sal_Unicode cDecSeparator,
125  sal_Int32 const * pGroups,
126  sal_Unicode cGroupSeparator,
127  bool bEraseTrailingDecZeros = false)
128 {
129  rtl_uString ** pData;
130  sal_Int32 * pCapacity;
131  rBuffer.accessInternals( &pData, &pCapacity );
132  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
133  eFormat, nDecPlaces, cDecSeparator, pGroups,
134  cGroupSeparator, bEraseTrailingDecZeros);
135 }
136 
140 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
141  rtl_math_StringFormat eFormat,
142  sal_Int32 nDecPlaces,
143  sal_Unicode cDecSeparator,
144  bool bEraseTrailingDecZeros = false)
145 {
146  rtl_uString ** pData;
147  sal_Int32 * pCapacity;
148  rBuffer.accessInternals( &pData, &pCapacity );
149  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
150  eFormat, nDecPlaces, cDecSeparator, NULL, 0,
151  bEraseTrailingDecZeros);
152 }
153 
156 #ifdef LIBO_INTERNAL_ONLY
157 inline double stringToDouble(std::string_view aString,
158  char cDecSeparator, char cGroupSeparator,
159  rtl_math_ConversionStatus * pStatus = NULL,
160  sal_Int32 * pParsedEnd = NULL)
161 {
162  char const * pBegin = aString.data();
163  char const * pEnd;
164  double fResult = rtl_math_stringToDouble(pBegin,
165  pBegin + aString.size(),
166  cDecSeparator, cGroupSeparator,
167  pStatus, &pEnd);
168  if (pParsedEnd != NULL)
169  *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
170  return fResult;
171 }
172 #else
173 inline double stringToDouble(rtl::OString const & rString,
174  char cDecSeparator, char cGroupSeparator,
175  rtl_math_ConversionStatus * pStatus = NULL,
176  sal_Int32 * pParsedEnd = NULL)
177 {
178  char const * pBegin = rString.getStr();
179  char const * pEnd;
180  double fResult = rtl_math_stringToDouble(pBegin,
181  pBegin + rString.getLength(),
182  cDecSeparator, cGroupSeparator,
183  pStatus, &pEnd);
184  if (pParsedEnd != NULL)
185  *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
186  return fResult;
187 }
188 #endif
189 
190 
193 #ifdef LIBO_INTERNAL_ONLY
194 inline double stringToDouble(std::u16string_view aString,
195  sal_Unicode cDecSeparator,
196  sal_Unicode cGroupSeparator,
197  rtl_math_ConversionStatus * pStatus = NULL,
198  sal_Int32 * pParsedEnd = NULL)
199 {
200  sal_Unicode const * pBegin = aString.data();
201  sal_Unicode const * pEnd;
202  double fResult = rtl_math_uStringToDouble(pBegin,
203  pBegin + aString.size(),
204  cDecSeparator, cGroupSeparator,
205  pStatus, &pEnd);
206  if (pParsedEnd != NULL)
207  *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
208  return fResult;
209 }
210 #else
211 inline double stringToDouble(rtl::OUString const & rString,
212  sal_Unicode cDecSeparator,
213  sal_Unicode cGroupSeparator,
214  rtl_math_ConversionStatus * pStatus = NULL,
215  sal_Int32 * pParsedEnd = NULL)
216 {
217  sal_Unicode const * pBegin = rString.getStr();
218  sal_Unicode const * pEnd;
219  double fResult = rtl_math_uStringToDouble(pBegin,
220  pBegin + rString.getLength(),
221  cDecSeparator, cGroupSeparator,
222  pStatus, &pEnd);
223  if (pParsedEnd != NULL)
224  *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
225  return fResult;
226 }
227 #endif
228 
231 inline double round(
232  double fValue, int nDecPlaces = 0,
234 {
235  return rtl_math_round(fValue, nDecPlaces, eMode);
236 }
237 
240 inline double pow10Exp(double fValue, int nExp)
241 {
242  return rtl_math_pow10Exp(fValue, nExp);
243 }
244 
247 inline double approxValue(double fValue)
248 {
249  return rtl_math_approxValue(fValue);
250 }
251 
254 inline double expm1(double fValue)
255 {
256  return rtl_math_expm1(fValue);
257 }
258 
261 inline double log1p(double fValue)
262 {
263  return rtl_math_log1p(fValue);
264 }
265 
268 inline double atanh(double fValue)
269 {
270  return rtl_math_atanh(fValue);
271 }
272 
275 inline double erf(double fValue)
276 {
277  return rtl_math_erf(fValue);
278 }
279 
282 inline double erfc(double fValue)
283 {
284  return rtl_math_erfc(fValue);
285 }
286 
289 inline double asinh(double fValue)
290 {
291  return rtl_math_asinh(fValue);
292 }
293 
296 inline double acosh(double fValue)
297 {
298  return rtl_math_acosh(fValue);
299 }
300 
303 inline bool approxEqual(double a, double b)
304 {
305  return rtl_math_approxEqual( a, b );
306 }
307 
313 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
314 {
315  if ( a == b )
316  return true;
317  double x = a - b;
318  return (x < 0.0 ? -x : x)
319  < ((a < 0.0 ? -a : a) * (1.0 / (pow(2.0, nPrec))));
320 }
321 
332 inline double approxAdd(double a, double b)
333 {
334  if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
335  && approxEqual( a, -b ) )
336  return 0.0;
337  return a + b;
338 }
339 
345 inline double approxSub(double a, double b)
346 {
347  if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
348  return 0.0;
349  return a - b;
350 }
351 
356 inline double approxFloor(double a)
357 {
358  return floor( approxValue( a ));
359 }
360 
365 inline double approxCeil(double a)
366 {
367  return ceil( approxValue( a ));
368 }
369 
372 inline bool isFinite(double d)
373 {
374  return SAL_MATH_FINITE(d);
375 }
376 
383 inline bool isInf(double d)
384 {
385  // exponent==0x7ff fraction==0
386  return !SAL_MATH_FINITE(d) &&
387  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
388  && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
389  == 0);
390 }
391 
394 inline bool isNan(double d)
395 {
396  // exponent==0x7ff fraction!=0
397  return !SAL_MATH_FINITE(d) && (
398  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
399  || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
400  != 0) );
401 }
402 
405 inline bool isSignBitSet(double d)
406 {
407  return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
408 }
409 
412 inline void setInf(double * pd, bool bNegative)
413 {
414  union
415  {
416  double sd;
417  sal_math_Double md;
418  };
419  md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
420  md.w32_parts.lsw = 0;
421  *pd = sd;
422 }
423 
426 inline void setNan(double * pd)
427 {
428  union
429  {
430  double sd;
431  sal_math_Double md;
432  };
433  md.w32_parts.msw = 0x7FFFFFFF;
434  md.w32_parts.lsw = 0xFFFFFFFF;
435  *pd = sd;
436 }
437 
447 inline bool isValidArcArg(double d)
448 {
449  return fabs(d)
450  <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
451  * static_cast< double >(static_cast< unsigned long >(0x80000000))
452  * 4);
453 }
454 
457 inline double sin(double d)
458 {
459  if ( isValidArcArg( d ) )
460  return ::sin( d );
461  setNan( &d );
462  return d;
463 }
464 
467 inline double cos(double d)
468 {
469  if ( isValidArcArg( d ) )
470  return ::cos( d );
471  setNan( &d );
472  return d;
473 }
474 
477 inline double tan(double d)
478 {
479  if ( isValidArcArg( d ) )
480  return ::tan( d );
481  setNan( &d );
482  return d;
483 }
484 
485 }
486 
487 }
488 
489 #endif // INCLUDED_RTL_MATH_HXX
490 
491 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:345
double round(double fValue, int nDecPlaces=0, rtl_math_RoundingMode eMode=rtl_math_RoundingMode_Corrected)
A wrapper around rtl_math_round.
Definition: math.hxx:231
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:365
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:282
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:51
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:372
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)).
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.
pData
New string from a character buffer array.
Definition: string.hxx:289
double cos(double d)
Safe cos(), returns NAN if not valid.
Definition: math.hxx:467
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:365
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:261
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:182
rtl_math_ConversionStatus
Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
Definition: math.h:67
SAL_DLLPUBLIC double rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C()
Rounds value to 15 significant decimal digits.
const char * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the characters of this string.
Definition: string.hxx:644
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1355
SAL_DLLPUBLIC double rtl_math_erf(double fValue) SAL_THROW_EXTERN_C()
Returns values of the Errorfunction erf.
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:399
sal_uInt16 sal_Unicode
Definition: types.h:103
bool approxEqual(double a, double b)
A wrapper around rtl_math_approxEqual.
Definition: math.hxx:303
bool isValidArcArg(double d)
If a value is a valid argument for sin(), cos(), tan().
Definition: math.hxx:447
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:618
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:196
double asinh(double fValue)
A wrapper around rtl_math_asinh.
Definition: math.hxx:289
rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat, sal_Int32 nDecPlaces, char cDecSeparator, sal_Int32 const *pGroups, char cGroupSeparator, bool bEraseTrailingDecZeros=false)
A wrapper around rtl_math_doubleToString.
Definition: math.hxx:24
SAL_DLLPUBLIC double rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic cosine.
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:477
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:240
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:332
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:806
SAL_DLLPUBLIC double rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C()
Returns values of the complement Errorfunction erfc.
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:412
pData
Definition: ustring.hxx:334
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:356
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...
bool isSignBitSet(double d)
If the sign bit is set.
Definition: math.hxx:405
SAL_DLLPUBLIC double 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...
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)
A wrapper around rtl_math_doubleToString that appends to an rtl::OStringBuffer.
Definition: math.hxx:56
double sin(double d)
Safe sin(), returns NAN if not valid.
Definition: math.hxx:457
rtl_math_StringFormat
Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString and rtl_math_doubleToUStrin...
Definition: math.h:20
double acosh(double fValue)
A wrapper around rtl_math_acosh.
Definition: math.hxx:296
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:784
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.
rtl_math_RoundingMode
Rounding modes for rtl_math_round.
Definition: math.h:84
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:394
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:89
Like HalfUp, but corrects roundoff errors, preferred.
Definition: math.h:88
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:383
double approxValue(double fValue)
A wrapper around rtl_math_approxValue.
Definition: math.hxx:247
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:254
void setNan(double *pd)
Set a QNAN.
Definition: math.hxx:426
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:121
void accessInternals(rtl_String ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OStringBuffer, for effective manipulation.
Definition: strbuf.hxx:1077
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...
double stringToDouble(rtl::OString const &rString, char cDecSeparator, char cGroupSeparator, rtl_math_ConversionStatus *pStatus=NULL, sal_Int32 *pParsedEnd=NULL)
A wrapper around rtl_math_stringToDouble.
Definition: math.hxx:173
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:275
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, char cDecSeparator, sal_Int32 const *pGroups, char cGroupSeparator, sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C()
Conversions analogous to sprintf() using internal rounding.
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:268
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:51
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 ...
SAL_DLLPUBLIC double rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic sine.
SAL_DLLPUBLIC double rtl_math_round(double fValue, int nDecPlaces, enum rtl_math_RoundingMode eMode) SAL_THROW_EXTERN_C()
Rounds a double value.