LibreOffice
LibreOffice 7.3 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/*
21 * This file is part of LibreOffice published API.
22 */
23
24#ifndef INCLUDED_RTL_MATH_HXX
25#define INCLUDED_RTL_MATH_HXX
26
27#include "rtl/math.h"
28#include "rtl/strbuf.hxx"
29#include "rtl/string.hxx"
30#include "rtl/ustring.hxx"
31#include "rtl/ustrbuf.hxx"
32#include "sal/mathconf.h"
33#include "sal/types.h"
34
35#include <cstddef>
36#include <math.h>
37
38namespace rtl {
39
40namespace math {
41
45 sal_Int32 nDecPlaces,
46 char cDecSeparator,
47 sal_Int32 const * pGroups,
48 char cGroupSeparator,
49 bool bEraseTrailingDecZeros = false)
50{
51 rtl::OString aResult;
52 rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
53 cDecSeparator, pGroups, cGroupSeparator,
54 bEraseTrailingDecZeros);
55 return aResult;
56}
57
61 sal_Int32 nDecPlaces,
62 char cDecSeparator,
63 bool bEraseTrailingDecZeros = false)
64{
65 rtl::OString aResult;
66 rtl_math_doubleToString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
67 cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
68 return aResult;
69}
70
77 rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
78 sal_Int32 nDecPlaces, char cDecSeparator, sal_Int32 const * pGroups,
79 char cGroupSeparator, bool bEraseTrailingDecZeros = false)
80{
81 rtl_String ** pData;
82 sal_Int32 * pCapacity;
83 rBuffer.accessInternals(&pData, &pCapacity);
85 pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
86 cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
87}
88
95 rtl::OStringBuffer& rBuffer, double fValue, rtl_math_StringFormat eFormat,
96 sal_Int32 nDecPlaces, char cDecSeparator,
97 bool bEraseTrailingDecZeros = false)
98{
99 rtl_String ** pData;
100 sal_Int32 * pCapacity;
101 rBuffer.accessInternals(&pData, &pCapacity);
103 pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
104 cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
105}
106
109inline rtl::OUString doubleToUString(double fValue,
110 rtl_math_StringFormat eFormat,
111 sal_Int32 nDecPlaces,
112 sal_Unicode cDecSeparator,
113 sal_Int32 const * pGroups,
114 sal_Unicode cGroupSeparator,
115 bool bEraseTrailingDecZeros = false)
116{
117 rtl::OUString aResult;
118 rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
119 cDecSeparator, pGroups, cGroupSeparator,
120 bEraseTrailingDecZeros);
121 return aResult;
122}
123
126inline rtl::OUString doubleToUString(double fValue,
127 rtl_math_StringFormat eFormat,
128 sal_Int32 nDecPlaces,
129 sal_Unicode cDecSeparator,
130 bool bEraseTrailingDecZeros = false)
131{
132 rtl::OUString aResult;
133 rtl_math_doubleToUString(&aResult.pData, NULL, 0, fValue, eFormat, nDecPlaces,
134 cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
135 return aResult;
136}
137
141inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
142 rtl_math_StringFormat eFormat,
143 sal_Int32 nDecPlaces,
144 sal_Unicode cDecSeparator,
145 sal_Int32 const * pGroups,
146 sal_Unicode cGroupSeparator,
147 bool bEraseTrailingDecZeros = false)
148{
149 rtl_uString ** pData;
150 sal_Int32 * pCapacity;
151 rBuffer.accessInternals( &pData, &pCapacity );
152 rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
153 eFormat, nDecPlaces, cDecSeparator, pGroups,
154 cGroupSeparator, bEraseTrailingDecZeros);
155}
156
160inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
161 rtl_math_StringFormat eFormat,
162 sal_Int32 nDecPlaces,
163 sal_Unicode cDecSeparator,
164 bool bEraseTrailingDecZeros = false)
165{
166 rtl_uString ** pData;
167 sal_Int32 * pCapacity;
168 rBuffer.accessInternals( &pData, &pCapacity );
169 rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
170 eFormat, nDecPlaces, cDecSeparator, NULL, 0,
171 bEraseTrailingDecZeros);
172}
173
176inline double stringToDouble(rtl::OString const & rString,
177 char cDecSeparator, char cGroupSeparator,
178 rtl_math_ConversionStatus * pStatus = NULL,
179 sal_Int32 * pParsedEnd = NULL)
180{
181 char const * pBegin = rString.getStr();
182 char const * pEnd;
183 double fResult = rtl_math_stringToDouble(pBegin,
184 pBegin + rString.getLength(),
185 cDecSeparator, cGroupSeparator,
186 pStatus, &pEnd);
187 if (pParsedEnd != NULL)
188 *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
189 return fResult;
190}
191
194inline double stringToDouble(rtl::OUString const & rString,
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 = rString.getStr();
201 sal_Unicode const * pEnd;
202 double fResult = rtl_math_uStringToDouble(pBegin,
203 pBegin + rString.getLength(),
204 cDecSeparator, cGroupSeparator,
205 pStatus, &pEnd);
206 if (pParsedEnd != NULL)
207 *pParsedEnd = static_cast<sal_Int32>(pEnd - pBegin);
208 return fResult;
209}
210
213inline double round(
214 double fValue, int nDecPlaces = 0,
216{
217 return rtl_math_round(fValue, nDecPlaces, eMode);
218}
219
222inline double pow10Exp(double fValue, int nExp)
223{
224 return rtl_math_pow10Exp(fValue, nExp);
225}
226
229inline double approxValue(double fValue)
230{
231 return rtl_math_approxValue(fValue);
232}
233
236inline double expm1(double fValue)
237{
238 return rtl_math_expm1(fValue);
239}
240
243inline double log1p(double fValue)
244{
245 return rtl_math_log1p(fValue);
246}
247
250inline double atanh(double fValue)
251{
252 return rtl_math_atanh(fValue);
253}
254
257inline double erf(double fValue)
258{
259 return rtl_math_erf(fValue);
260}
261
264inline double erfc(double fValue)
265{
266 return rtl_math_erfc(fValue);
267}
268
271inline double asinh(double fValue)
272{
273 return rtl_math_asinh(fValue);
274}
275
278inline double acosh(double fValue)
279{
280 return rtl_math_acosh(fValue);
281}
282
285inline bool approxEqual(double a, double b)
286{
287 return rtl_math_approxEqual( a, b );
288}
289
295inline bool approxEqual(double a, double b, sal_Int16 nPrec)
296{
297 if ( a == b )
298 return true;
299 double x = a - b;
300 return (x < 0.0 ? -x : x)
301 < ((a < 0.0 ? -a : a) * (1.0 / (pow(2.0, nPrec))));
302}
303
314inline double approxAdd(double a, double b)
315{
316 if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
317 && approxEqual( a, -b ) )
318 return 0.0;
319 return a + b;
320}
321
327inline double approxSub(double a, double b)
328{
329 if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
330 return 0.0;
331 return a - b;
332}
333
338inline double approxFloor(double a)
339{
340 return floor( approxValue( a ));
341}
342
347inline double approxCeil(double a)
348{
349 return ceil( approxValue( a ));
350}
351
354inline bool isFinite(double d)
355{
356 return SAL_MATH_FINITE(d);
357}
358
365inline bool isInf(double d)
366{
367 // exponent==0x7ff fraction==0
368 return !SAL_MATH_FINITE(d) &&
369 (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
370 && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
371 == 0);
372}
373
376inline bool isNan(double d)
377{
378 // exponent==0x7ff fraction!=0
379 return !SAL_MATH_FINITE(d) && (
380 (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
381 || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
382 != 0) );
383}
384
387inline bool isSignBitSet(double d)
388{
389 return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
390}
391
394inline void setInf(double * pd, bool bNegative)
395{
396 union
397 {
398 double sd;
399 sal_math_Double md;
400 };
401 md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
402 md.w32_parts.lsw = 0;
403 *pd = sd;
404}
405
408inline void setNan(double * pd)
409{
410 union
411 {
412 double sd;
413 sal_math_Double md;
414 };
415 md.w32_parts.msw = 0x7FFFFFFF;
416 md.w32_parts.lsw = 0xFFFFFFFF;
417 *pd = sd;
418}
419
429inline bool isValidArcArg(double d)
430{
431 return fabs(d)
432 <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
433 * static_cast< double >(static_cast< unsigned long >(0x80000000))
434 * 4);
435}
436
439inline double sin(double d)
440{
441 if ( isValidArcArg( d ) )
442 return ::sin( d );
443 setNan( &d );
444 return d;
445}
446
449inline double cos(double d)
450{
451 if ( isValidArcArg( d ) )
452 return ::cos( d );
453 setNan( &d );
454 return d;
455}
456
459inline double tan(double d)
460{
461 if ( isValidArcArg( d ) )
462 return ::tan( d );
463 setNan( &d );
464 return d;
465}
466
467}
468
469}
470
471#endif // INCLUDED_RTL_MATH_HXX
472
473/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
sal_uInt16 sal_Unicode
Definition: types.h:123
SAL_DLLPUBLIC double rtl_math_erf(double fValue) SAL_THROW_EXTERN_C()
Returns values of the Errorfunction erf.
rtl_math_RoundingMode
Rounding modes for rtl_math_round.
Definition: math.h:105
@ rtl_math_RoundingMode_Corrected
Like HalfUp, but corrects roundoff errors, preferred.
Definition: math.h:108
rtl_math_ConversionStatus
Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
Definition: math.h:88
SAL_DLLPUBLIC double rtl_math_round(double fValue, int nDecPlaces, enum rtl_math_RoundingMode eMode) SAL_THROW_EXTERN_C()
Rounds a double value.
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_StringFormat
Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString and rtl_math_doubleToUStrin...
Definition: math.h:41
SAL_DLLPUBLIC double rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic sine.
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.
SAL_DLLPUBLIC double rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C()
Returns values of the complement Errorfunction erfc.
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_approxValue(double fValue) SAL_THROW_EXTERN_C()
Rounds value to 15 significant decimal digits.
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_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.
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,...
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.
SAL_DLLPUBLIC double rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic cosine.
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_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.
Definition: unotype.hxx:47
bool approxEqual(double a, double b)
A wrapper around rtl_math_approxEqual.
Definition: math.hxx:285
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:314
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:376
double sin(double d)
Safe sin(), returns NAN if not valid.
Definition: math.hxx:439
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:243
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:257
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:327
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:176
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:338
double asinh(double fValue)
A wrapper around rtl_math_asinh.
Definition: math.hxx:271
bool isValidArcArg(double d)
If a value is a valid argument for sin(), cos(), tan().
Definition: math.hxx:429
void setNan(double *pd)
Set a QNAN.
Definition: math.hxx:408
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:236
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:44
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:141
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:109
double round(double fValue, int nDecPlaces=0, rtl_math_RoundingMode eMode=rtl_math_RoundingMode_Corrected)
A wrapper around rtl_math_round.
Definition: math.hxx:213
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:365
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:264
double approxValue(double fValue)
A wrapper around rtl_math_approxValue.
Definition: math.hxx:229
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:76
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:250
bool isSignBitSet(double d)
If the sign bit is set.
Definition: math.hxx:387
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:394
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:459
double acosh(double fValue)
A wrapper around rtl_math_acosh.
Definition: math.hxx:278
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:222
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:354
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:347
double cos(double d)
Safe cos(), returns NAN if not valid.
Definition: math.hxx:449
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:70
void accessInternals(rtl_String ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OStringBuffer, for effective manipulation.
Definition: strbuf.hxx:1082
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:370
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:181
const char * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the characters of this string.
Definition: string.hxx:603
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:577
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:70
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:411
void accessInternals(rtl_uString ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OUStringBuffer, for effective manipulation.
Definition: ustrbuf.hxx:1354
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:204
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:814
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:792