LibreOffice
LibreOffice 5.3 SDK C/C++ API Reference
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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  * 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 
244 inline bool approxEqual(double a, double b)
245 {
246  return rtl_math_approxEqual( a, b );
247 }
248 
254 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
255 {
256  if ( a == b )
257  return true;
258  double x = a - b;
259  return (x < 0.0 ? -x : x)
260  < ((a < 0.0 ? -a : a) * (1.0 / (pow(static_cast<double>(2.0), nPrec))));
261 }
262 
273 inline double approxAdd(double a, double b)
274 {
275  if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
276  && approxEqual( a, -b ) )
277  return 0.0;
278  return a + b;
279 }
280 
286 inline double approxSub(double a, double b)
287 {
288  if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
289  return 0.0;
290  return a - b;
291 }
292 
297 inline double approxFloor(double a)
298 {
299  return floor( approxValue( a ));
300 }
301 
306 inline double approxCeil(double a)
307 {
308  return ceil( approxValue( a ));
309 }
310 
313 inline bool isFinite(double d)
314 {
315  return SAL_MATH_FINITE(d);
316 }
317 
324 inline bool isInf(double d)
325 {
326  // exponent==0x7ff fraction==0
327  return !SAL_MATH_FINITE(d) &&
328  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
329  && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
330  == 0);
331 }
332 
335 inline bool isNan(double d)
336 {
337  // exponent==0x7ff fraction!=0
338  return !SAL_MATH_FINITE(d) && (
339  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
340  || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
341  != 0) );
342 }
343 
346 inline bool isSignBitSet(double d)
347 {
348  return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
349 }
350 
353 inline void setInf(double * pd, bool bNegative)
354 {
355  union
356  {
357  double sd;
358  sal_math_Double md;
359  };
360  md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
361  md.w32_parts.lsw = 0;
362  *pd = sd;
363 }
364 
367 inline void setNan(double * pd)
368 {
369  union
370  {
371  double sd;
372  sal_math_Double md;
373  };
374  md.w32_parts.msw = 0x7FFFFFFF;
375  md.w32_parts.lsw = 0xFFFFFFFF;
376  *pd = sd;
377 }
378 
388 inline bool isValidArcArg(double d)
389 {
390  return fabs(d)
391  <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
392  * static_cast< double >(static_cast< unsigned long >(0x80000000))
393  * 2);
394 }
395 
398 inline double sin(double d)
399 {
400  if ( isValidArcArg( d ) )
401  return ::sin( d );
402  setNan( &d );
403  return d;
404 }
405 
408 inline double cos(double d)
409 {
410  if ( isValidArcArg( d ) )
411  return ::cos( d );
412  setNan( &d );
413  return d;
414 }
415 
418 inline double tan(double d)
419 {
420  if ( isValidArcArg( d ) )
421  return ::tan( d );
422  setNan( &d );
423  return d;
424 }
425 
426 }
427 
428 }
429 
430 #endif // INCLUDED_RTL_MATH_HXX
431 
432 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool approxEqual(double a, double b)
A wrapper around rtl_math_approxEqual.
Definition: math.hxx:244
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:286
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:324
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:628
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:606
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: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:408
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:329
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:353
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:297
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:273
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:313
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1241
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:398
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.hxx:430
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:418
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:346
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:388
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:367
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.
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:404
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:335
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:306