LibreOffice LibreOffice 4.1 SDK C/C++ API Reference

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

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 <math.h>
31
32 namespace rtl {
33
34 namespace math {
35
38 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
39 sal_Int32 nDecPlaces,
40 sal_Char cDecSeparator,
41 sal_Int32 const * pGroups,
42 sal_Char cGroupSeparator,
43 bool bEraseTrailingDecZeros = false)
44 {
45 rtl::OString aResult;
46 rtl_math_doubleToString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
47 cDecSeparator, pGroups, cGroupSeparator,
48 bEraseTrailingDecZeros);
49 return aResult;
50 }
51
54 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
55 sal_Int32 nDecPlaces,
56 sal_Char cDecSeparator,
57 bool bEraseTrailingDecZeros = false)
58 {
59 rtl::OString aResult;
60 rtl_math_doubleToString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
61 cDecSeparator, 0, 0, bEraseTrailingDecZeros);
62 return aResult;
63 }
64
67 inline rtl::OUString doubleToUString(double fValue,
68 rtl_math_StringFormat eFormat,
69 sal_Int32 nDecPlaces,
70 sal_Unicode cDecSeparator,
71 sal_Int32 const * pGroups,
72 sal_Unicode cGroupSeparator,
73 bool bEraseTrailingDecZeros = false)
74 {
75 rtl::OUString aResult;
76 rtl_math_doubleToUString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
77 cDecSeparator, pGroups, cGroupSeparator,
78 bEraseTrailingDecZeros);
79 return aResult;
80 }
81
84 inline rtl::OUString doubleToUString(double fValue,
85 rtl_math_StringFormat eFormat,
86 sal_Int32 nDecPlaces,
87 sal_Unicode cDecSeparator,
88 bool bEraseTrailingDecZeros = false)
89 {
90 rtl::OUString aResult;
91 rtl_math_doubleToUString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
92 cDecSeparator, 0, 0, bEraseTrailingDecZeros);
93 return aResult;
94 }
95
99 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
100 rtl_math_StringFormat eFormat,
101 sal_Int32 nDecPlaces,
102 sal_Unicode cDecSeparator,
103 sal_Int32 const * pGroups,
104 sal_Unicode cGroupSeparator,
105 bool bEraseTrailingDecZeros = false)
106 {
107 rtl_uString ** pData;
108 sal_Int32 * pCapacity;
109 rBuffer.accessInternals( &pData, &pCapacity );
110 rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
111 eFormat, nDecPlaces, cDecSeparator, pGroups,
112 cGroupSeparator, bEraseTrailingDecZeros);
113 }
114
118 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
119 rtl_math_StringFormat eFormat,
120 sal_Int32 nDecPlaces,
121 sal_Unicode cDecSeparator,
122 bool bEraseTrailingDecZeros = false)
123 {
124 rtl_uString ** pData;
125 sal_Int32 * pCapacity;
126 rBuffer.accessInternals( &pData, &pCapacity );
127 rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
128 eFormat, nDecPlaces, cDecSeparator, 0, 0,
129 bEraseTrailingDecZeros);
130 }
131
134 inline double stringToDouble(rtl::OString const & rString,
135 sal_Char cDecSeparator, sal_Char cGroupSeparator,
136 rtl_math_ConversionStatus * pStatus = 0,
137 sal_Int32 * pParsedEnd = 0)
138 {
139 sal_Char const * pBegin = rString.getStr();
140 sal_Char const * pEnd;
141 double fResult = rtl_math_stringToDouble(pBegin,
142 pBegin + rString.getLength(),
143 cDecSeparator, cGroupSeparator,
144 pStatus, &pEnd);
145 if (pParsedEnd != 0)
146 *pParsedEnd = (sal_Int32)(pEnd - pBegin);
147 return fResult;
148 }
149
152 inline double stringToDouble(rtl::OUString const & rString,
153 sal_Unicode cDecSeparator,
154 sal_Unicode cGroupSeparator,
155 rtl_math_ConversionStatus * pStatus = 0,
156 sal_Int32 * pParsedEnd = 0)
157 {
158 sal_Unicode const * pBegin = rString.getStr();
159 sal_Unicode const * pEnd;
160 double fResult = rtl_math_uStringToDouble(pBegin,
161 pBegin + rString.getLength(),
162 cDecSeparator, cGroupSeparator,
163 pStatus, &pEnd);
164 if (pParsedEnd != 0)
165 *pParsedEnd = (sal_Int32)(pEnd - pBegin);
166 return fResult;
167 }
168
171 inline double round(
172 double fValue, int nDecPlaces = 0,
173 rtl_math_RoundingMode eMode = rtl_math_RoundingMode_Corrected)
174 {
175 return rtl_math_round(fValue, nDecPlaces, eMode);
176 }
177
180 inline double pow10Exp(double fValue, int nExp)
181 {
182 return rtl_math_pow10Exp(fValue, nExp);
183 }
184
187 inline double approxValue(double fValue)
188 {
189 return rtl_math_approxValue(fValue);
190 }
191
194 inline double expm1(double fValue)
195 {
196 return rtl_math_expm1(fValue);
197 }
198
201 inline double log1p(double fValue)
202 {
203 return rtl_math_log1p(fValue);
204 }
205
208 inline double atanh(double fValue)
209 {
210 return rtl_math_atanh(fValue);
211 }
212
215 inline double erf(double fValue)
216 {
217 return rtl_math_erf(fValue);
218 }
219
222 inline double erfc(double fValue)
223 {
224 return rtl_math_erfc(fValue);
225 }
226
229 inline double asinh(double fValue)
230 {
231 return rtl_math_asinh(fValue);
232 }
233
236 inline double acosh(double fValue)
237 {
238 return rtl_math_acosh(fValue);
239 }
240
241
248 inline bool approxEqual(double a, double b)
249 {
250 if ( a == b )
251 return true;
252 double x = a - b;
253 return (x < 0.0 ? -x : x)
254 < ((a < 0.0 ? -a : a) * (1.0 / (16777216.0 * 16777216.0)));
255 }
256
262 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
263 {
264 if ( a == b )
265 return true;
266 double x = a - b;
267 return (x < 0.0 ? -x : x)
268 < ((a < 0.0 ? -a : a) * (1.0 / (pow(static_cast<double>(2.0), nPrec))));
269 }
280 inline double approxAdd(double a, double b)
281 {
282 if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
283 && approxEqual( a, -b ) )
284 return 0.0;
285 return a + b;
286 }
287
293 inline double approxSub(double a, double b)
294 {
295 if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
296 return 0.0;
297 return a - b;
298 }
299
304 inline double approxFloor(double a)
305 {
306 return floor( approxValue( a ));
307 }
308
313 inline double approxCeil(double a)
314 {
315 return ceil( approxValue( a ));
316 }
317
320 inline bool isFinite(double d)
321 {
322 return SAL_MATH_FINITE(d) != 0;
323 }
324
331 inline bool isInf(double d)
332 {
333 // exponent==0x7ff fraction==0
334 return (SAL_MATH_FINITE(d) == 0) &&
335 (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
336 && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
337 == 0);
338 }
339
342 inline bool isNan(double d)
343 {
344 // exponent==0x7ff fraction!=0
345 return (SAL_MATH_FINITE(d) == 0) && (
346 (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
347 || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
348 != 0) );
349 }
350
353 inline bool isSignBitSet(double d)
354 {
355 return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
356 }
357
360 inline void setInf(double * pd, bool bNegative)
361 {
362 union
363 {
364 double sd;
365 sal_math_Double md;
366 };
367 md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
368 md.w32_parts.lsw = 0;
369 *pd = sd;
370 }
371
374 inline void setNan(double * pd)
375 {
376 union
377 {
378 double sd;
379 sal_math_Double md;
380 };
381 md.w32_parts.msw = 0x7FFFFFFF;
382 md.w32_parts.lsw = 0xFFFFFFFF;
383 *pd = sd;
384 }
385
395 inline bool isValidArcArg(double d)
396 {
397 return fabs(d)
398 <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
399 * static_cast< double >(static_cast< unsigned long >(0x80000000))
400 * 2);
401 }
402
405 inline double sin(double d)
406 {
407 if ( isValidArcArg( d ) )
408 return ::sin( d );
409 setNan( &d );
410 return d;
411 }
412
415 inline double cos(double d)
416 {
417 if ( isValidArcArg( d ) )
418 return ::cos( d );
419 setNan( &d );
420 return d;
421 }
422
425 inline double tan(double d)
426 {
427 if ( isValidArcArg( d ) )
428 return ::tan( d );
429 setNan( &d );
430 return d;
431 }
432
433 }
434
435 }
436
437 #endif // INCLUDED_RTL_MATH_HXX
438
439 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rtl_math_erfc
SAL_DLLPUBLIC double rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C()
Returns values of the complement Errorfunction erfc.
rtl_math_stringToDouble
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...
rtl_math_round
SAL_DLLPUBLIC double rtl_math_round(double fValue, int nDecPlaces, enum rtl_math_RoundingMode eMode) SAL_THROW_EXTERN_C()
Rounds a double value.
rtl::math::asinh
double asinh(double fValue)
A wrapper around rtl_math_asinh.
Definition: math.hxx:229
rtl::math::log1p
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:201
rtl::math::erfc
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:222
rtl::math::approxCeil
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:313
rtl_math_RoundingMode
rtl_math_RoundingMode
Rounding modes for rtl_math_round.
Definition: math.h:80
rtl::math::approxSub
double approxSub(double a, double b)
Substract two values (a-b).
Definition: math.hxx:293
sal_Unicode
sal_uInt16 sal_Unicode
Definition: types.h:134
rtl::math::doubleToUString
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:67
rtl_math_approxValue
SAL_DLLPUBLIC double rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C()
Rounds value to 15 significant decimal digits.
rtl::math::expm1
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:194
rtl_math_uStringToDouble
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...
math.h
rtl::math::approxEqual
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:248
rtl::OString::getStr
const sal_Char * getStr() const
Returns a pointer to the characters of this string.
Definition: string.hxx:367
rtl::math::approxFloor
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:304
rtl_math_expm1
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.
rtl::math::isSignBitSet
bool isSignBitSet(double d)
If the sign bit is set.
Definition: math.hxx:353
mathconf.h
rtl::OString::getLength
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:341
rtl_math_asinh
SAL_DLLPUBLIC double rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic sine.
rtl_math_acosh
SAL_DLLPUBLIC double rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic cosine.
rtl::math::isValidArcArg
bool isValidArcArg(double d)
If a value is a valid argument for sin(), cos(), tan().
Definition: math.hxx:395
rtl::OUString::getLength
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:408
rtl::OUStringBuffer::getLength
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:247
rtl::math::pow10Exp
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:180
rtl::math::approxAdd
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:280
rtl_math_doubleToString
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.
rtl::OUString::pData
pData
Definition: ustring.hxx:200
ustring.hxx
rtl::math::erf
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:215
sal_Char
char sal_Char
Definition: types.h:122
rtl::OUStringBuffer
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:92
rtl::math::sin
double sin(double d)
Safe sin(), returns NAN if not valid.
Definition: math.hxx:405
rtl::math::approxValue
double approxValue(double fValue)
A wrapper around rtl_math_approxValue.
Definition: math.hxx:187
rtl::math::stringToDouble
double stringToDouble(rtl::OString const &rString, sal_Char cDecSeparator, sal_Char cGroupSeparator, rtl_math_ConversionStatus *pStatus=0, sal_Int32 *pParsedEnd=0)
A wrapper around rtl_math_stringToDouble.
Definition: math.hxx:134
rtl::math::isNan
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:342
types.h
rtl::math::doubleToUStringBuffer
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:99
rtl_math_RoundingMode_Corrected
Like HalfUp, but corrects roundoff errors, preferred.
Definition: math.h:84
rtl::math::doubleToString
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:38
rtl::math::round
double round(double fValue, int nDecPlaces=0, rtl_math_RoundingMode eMode=rtl_math_RoundingMode_Corrected)
A wrapper around rtl_math_round.
Definition: math.hxx:171
rtl_math_atanh
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)).
rtl::math::setInf
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:360
rtl_math_erf
SAL_DLLPUBLIC double rtl_math_erf(double fValue) SAL_THROW_EXTERN_C()
Returns values of the Errorfunction erf.
rtl::math::acosh
double acosh(double fValue)
A wrapper around rtl_math_acosh.
Definition: math.hxx:236
rtl::OUString
This String class provides base functionality for C++ like Unicode character array handling...
Definition: ustring.hxx:87
rtl::math::isFinite
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:320
rtl::math::setNan
void setNan(double *pd)
Set a QNAN.
Definition: math.hxx:374
rtl_math_pow10Exp
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...
rtl::math::isInf
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:331
rtl_math_doubleToUString
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_log1p
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.
rtl::OString
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:90
rtl_math_StringFormat
rtl_math_StringFormat
Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString and rtl_math_doubleToUStrin...
Definition: math.h:36
rtl::OUStringBuffer::accessInternals
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1055
rtl::OUString::getStr
const sal_Unicode * getStr() const
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:430
string.hxx
rtl::math::atanh
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:208
rtl_math_ConversionStatus
rtl_math_ConversionStatus
Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
Definition: math.h:63
rtl::math::tan
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:425
rtl::math::cos
double cos(double d)
Safe cos(), returns NAN if not valid.
Definition: math.hxx:415
ustrbuf.hxx