LibreOffice
LibreOffice 5.1 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/string.hxx>
25 #include <rtl/ustring.hxx>
26 #include <rtl/ustrbuf.hxx>
27 #include <sal/mathconf.h>
28 #include <sal/types.h>
29 
30 #include <cstddef>
31 #include <math.h>
32 
33 namespace rtl {
34 
35 namespace math {
36 
39 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
40  sal_Int32 nDecPlaces,
41  sal_Char cDecSeparator,
42  sal_Int32 const * pGroups,
43  sal_Char cGroupSeparator,
44  bool bEraseTrailingDecZeros = false)
45 {
46  rtl::OString aResult;
47  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
48  cDecSeparator, pGroups, cGroupSeparator,
49  bEraseTrailingDecZeros);
50  return aResult;
51 }
52 
55 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
56  sal_Int32 nDecPlaces,
57  sal_Char cDecSeparator,
58  bool bEraseTrailingDecZeros = false)
59 {
60  rtl::OString aResult;
61  rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
62  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
63  return aResult;
64 }
65 
68 inline rtl::OUString doubleToUString(double fValue,
69  rtl_math_StringFormat eFormat,
70  sal_Int32 nDecPlaces,
71  sal_Unicode cDecSeparator,
72  sal_Int32 const * pGroups,
73  sal_Unicode cGroupSeparator,
74  bool bEraseTrailingDecZeros = false)
75 {
76  rtl::OUString aResult;
77  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
78  cDecSeparator, pGroups, cGroupSeparator,
79  bEraseTrailingDecZeros);
80  return aResult;
81 }
82 
85 inline rtl::OUString doubleToUString(double fValue,
86  rtl_math_StringFormat eFormat,
87  sal_Int32 nDecPlaces,
88  sal_Unicode cDecSeparator,
89  bool bEraseTrailingDecZeros = false)
90 {
91  rtl::OUString aResult;
92  rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
93  cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
94  return aResult;
95 }
96 
100 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
101  rtl_math_StringFormat eFormat,
102  sal_Int32 nDecPlaces,
103  sal_Unicode cDecSeparator,
104  sal_Int32 const * pGroups,
105  sal_Unicode cGroupSeparator,
106  bool bEraseTrailingDecZeros = false)
107 {
108  rtl_uString ** pData;
109  sal_Int32 * pCapacity;
110  rBuffer.accessInternals( &pData, &pCapacity );
111  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
112  eFormat, nDecPlaces, cDecSeparator, pGroups,
113  cGroupSeparator, bEraseTrailingDecZeros);
114 }
115 
119 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
120  rtl_math_StringFormat eFormat,
121  sal_Int32 nDecPlaces,
122  sal_Unicode cDecSeparator,
123  bool bEraseTrailingDecZeros = false)
124 {
125  rtl_uString ** pData;
126  sal_Int32 * pCapacity;
127  rBuffer.accessInternals( &pData, &pCapacity );
128  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
129  eFormat, nDecPlaces, cDecSeparator, NULL, 0,
130  bEraseTrailingDecZeros);
131 }
132 
135 inline double stringToDouble(rtl::OString const & rString,
136  sal_Char cDecSeparator, sal_Char cGroupSeparator,
137  rtl_math_ConversionStatus * pStatus = NULL,
138  sal_Int32 * pParsedEnd = NULL)
139 {
140  sal_Char const * pBegin = rString.getStr();
141  sal_Char const * pEnd;
142  double fResult = rtl_math_stringToDouble(pBegin,
143  pBegin + rString.getLength(),
144  cDecSeparator, cGroupSeparator,
145  pStatus, &pEnd);
146  if (pParsedEnd != NULL)
147  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
148  return fResult;
149 }
150 
153 inline double stringToDouble(rtl::OUString const & rString,
154  sal_Unicode cDecSeparator,
155  sal_Unicode cGroupSeparator,
156  rtl_math_ConversionStatus * pStatus = NULL,
157  sal_Int32 * pParsedEnd = NULL)
158 {
159  sal_Unicode const * pBegin = rString.getStr();
160  sal_Unicode const * pEnd;
161  double fResult = rtl_math_uStringToDouble(pBegin,
162  pBegin + rString.getLength(),
163  cDecSeparator, cGroupSeparator,
164  pStatus, &pEnd);
165  if (pParsedEnd != NULL)
166  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
167  return fResult;
168 }
169 
172 inline double round(
173  double fValue, int nDecPlaces = 0,
175 {
176  return rtl_math_round(fValue, nDecPlaces, eMode);
177 }
178 
181 inline double pow10Exp(double fValue, int nExp)
182 {
183  return rtl_math_pow10Exp(fValue, nExp);
184 }
185 
188 inline double approxValue(double fValue)
189 {
190  return rtl_math_approxValue(fValue);
191 }
192 
195 inline double expm1(double fValue)
196 {
197  return rtl_math_expm1(fValue);
198 }
199 
202 inline double log1p(double fValue)
203 {
204  return rtl_math_log1p(fValue);
205 }
206 
209 inline double atanh(double fValue)
210 {
211  return rtl_math_atanh(fValue);
212 }
213 
216 inline double erf(double fValue)
217 {
218  return rtl_math_erf(fValue);
219 }
220 
223 inline double erfc(double fValue)
224 {
225  return rtl_math_erfc(fValue);
226 }
227 
230 inline double asinh(double fValue)
231 {
232  return rtl_math_asinh(fValue);
233 }
234 
237 inline double acosh(double fValue)
238 {
239  return rtl_math_acosh(fValue);
240 }
241 
242 
249 inline bool approxEqual(double a, double b)
250 {
251  if ( a == b )
252  return true;
253  double x = a - b;
254  return (x < 0.0 ? -x : x)
255  < ((a < 0.0 ? -a : a) * (1.0 / (16777216.0 * 16777216.0)));
256 }
257 
263 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
264 {
265  if ( a == b )
266  return true;
267  double x = a - b;
268  return (x < 0.0 ? -x : x)
269  < ((a < 0.0 ? -a : a) * (1.0 / (pow(static_cast<double>(2.0), nPrec))));
270 }
281 inline double approxAdd(double a, double b)
282 {
283  if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
284  && approxEqual( a, -b ) )
285  return 0.0;
286  return a + b;
287 }
288 
294 inline double approxSub(double a, double b)
295 {
296  if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
297  return 0.0;
298  return a - b;
299 }
300 
305 inline double approxFloor(double a)
306 {
307  return floor( approxValue( a ));
308 }
309 
314 inline double approxCeil(double a)
315 {
316  return ceil( approxValue( a ));
317 }
318 
321 inline bool isFinite(double d)
322 {
323  return SAL_MATH_FINITE(d);
324 }
325 
332 inline bool isInf(double d)
333 {
334  // exponent==0x7ff fraction==0
335  return !SAL_MATH_FINITE(d) &&
336  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
337  && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
338  == 0);
339 }
340 
343 inline bool isNan(double d)
344 {
345  // exponent==0x7ff fraction!=0
346  return !SAL_MATH_FINITE(d) && (
347  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
348  || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
349  != 0) );
350 }
351 
354 inline bool isSignBitSet(double d)
355 {
356  return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
357 }
358 
361 inline void setInf(double * pd, bool bNegative)
362 {
363  union
364  {
365  double sd;
366  sal_math_Double md;
367  };
368  md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
369  md.w32_parts.lsw = 0;
370  *pd = sd;
371 }
372 
375 inline void setNan(double * pd)
376 {
377  union
378  {
379  double sd;
380  sal_math_Double md;
381  };
382  md.w32_parts.msw = 0x7FFFFFFF;
383  md.w32_parts.lsw = 0xFFFFFFFF;
384  *pd = sd;
385 }
386 
396 inline bool isValidArcArg(double d)
397 {
398  return fabs(d)
399  <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
400  * static_cast< double >(static_cast< unsigned long >(0x80000000))
401  * 2);
402 }
403 
406 inline double sin(double d)
407 {
408  if ( isValidArcArg( d ) )
409  return ::sin( d );
410  setNan( &d );
411  return d;
412 }
413 
416 inline double cos(double d)
417 {
418  if ( isValidArcArg( d ) )
419  return ::cos( d );
420  setNan( &d );
421  return d;
422 }
423 
426 inline double tan(double d)
427 {
428  if ( isValidArcArg( d ) )
429  return ::tan( d );
430  setNan( &d );
431  return d;
432 }
433 
434 }
435 
436 }
437 
438 #endif // INCLUDED_RTL_MATH_HXX
439 
440 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool approxEqual(double a, double b)
Test equality of two values with an accuracy of the magnitude of the given values scaled by 2^-48 (4 ...
Definition: math.hxx:249
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:294
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:332
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:209
const sal_Unicode * getStr() const
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:528
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:135
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:506
char sal_Char
A legacy synonym for char.
Definition: types.h:130
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:416
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:223
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:235
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:361
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:305
double round(double fValue, int nDecPlaces=0, rtl_math_RoundingMode eMode=rtl_math_RoundingMode_Corrected)
A wrapper around rtl_math_round.
Definition: math.hxx:172
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:181
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:281
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)).
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:321
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1118
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:230
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:406
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.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:68
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:426
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:188
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:354
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:39
sal_uInt16 sal_Unicode
Definition: types.h:155
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:396
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:195
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:237
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:375
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.
Definition: bootstrap.hxx:29
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:106
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:368
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:216
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:343
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:100
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:56
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:202
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:90
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:314