LibreOffice: rtl/math.hxx Source File

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
40 namespace math {
41
44 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
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
60 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
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
76 inline void doubleToStringBuffer(
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);
84 rtl_math_doubleToString(
85 pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
86 cDecSeparator, pGroups, cGroupSeparator, bEraseTrailingDecZeros);
87}
88
94 inline void doubleToStringBuffer(
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);
102 rtl_math_doubleToString(
103 pData, pCapacity, rBuffer.getLength(), fValue, eFormat, nDecPlaces,
104 cDecSeparator, NULL, 0, bEraseTrailingDecZeros);
105}
106
109 inline 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
126 inline 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
141 inline 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
160 inline 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
176 inline 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
194 inline 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
213 inline double round(
214 double fValue, int nDecPlaces = 0,
215 rtl_math_RoundingMode eMode = rtl_math_RoundingMode_Corrected)
216{
217 return rtl_math_round(fValue, nDecPlaces, eMode);
218}
219
222 inline double pow10Exp(double fValue, int nExp)
223{
224 return rtl_math_pow10Exp(fValue, nExp);
225}
226
229 inline double approxValue(double fValue)
230{
231 return rtl_math_approxValue(fValue);
232}
233
236 inline double expm1(double fValue)
237{
238 return rtl_math_expm1(fValue);
239}
240
243 inline double log1p(double fValue)
244{
245 return rtl_math_log1p(fValue);
246}
247
250 inline double atanh(double fValue)
251{
252 return rtl_math_atanh(fValue);
253}
254
257 inline double erf(double fValue)
258{
259 return rtl_math_erf(fValue);
260}
261
264 inline double erfc(double fValue)
265{
266 return rtl_math_erfc(fValue);
267}
268
271 inline double asinh(double fValue)
272{
273 return rtl_math_asinh(fValue);
274}
275
278 inline double acosh(double fValue)
279{
280 return rtl_math_acosh(fValue);
281}
282
285 inline bool approxEqual(double a, double b)
286{
287 return rtl_math_approxEqual( a, b );
288}
289
295 inline 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
314 inline 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
327 inline 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
338 inline double approxFloor(double a)
339{
340 return floor( approxValue( a ));
341}
342
347 inline double approxCeil(double a)
348{
349 return ceil( approxValue( a ));
350}
351
354 inline bool isFinite(double d)
355{
356 return SAL_MATH_FINITE(d);
357}
358
365 inline 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
376 inline 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
387 inline bool isSignBitSet(double d)
388{
389 return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
390}
391
394 inline 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
408 inline 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
429 inline 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
439 inline double sin(double d)
440{
441 if ( isValidArcArg( d ) )
442 return ::sin( d );
443 setNan( &d );
444 return d;
445}
446
449 inline double cos(double d)
450{
451 if ( isValidArcArg( d ) )
452 return ::cos( d );
453 setNan( &d );
454 return d;
455}
456
459 inline 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: */
mathconf.h
types.h
sal_Unicode
sal_uInt16 sal_Unicode
Definition: types.h:123
ustring.hxx
math.h
rtl_math_erf
SAL_DLLPUBLIC double rtl_math_erf(double fValue) SAL_THROW_EXTERN_C()
Returns values of the Errorfunction erf.
rtl_math_RoundingMode
rtl_math_RoundingMode
Rounding modes for rtl_math_round.
Definition: math.h:105
rtl_math_RoundingMode_Corrected
@ rtl_math_RoundingMode_Corrected
Like HalfUp, but corrects roundoff errors, preferred.
Definition: math.h:108
rtl_math_ConversionStatus
rtl_math_ConversionStatus
Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
Definition: math.h:88
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_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_StringFormat
rtl_math_StringFormat
Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString and rtl_math_doubleToUStrin...
Definition: math.h:41
rtl_math_asinh
SAL_DLLPUBLIC double rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic sine.
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, char cDecSeparator, sal_Int32 const *pGroups, char cGroupSeparator, sal_Bool bEraseTrailingDecZeros) SAL_THROW_EXTERN_C()
Conversions analogous to sprintf() using internal rounding.
rtl_math_erfc
SAL_DLLPUBLIC double rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C()
Returns values of the complement Errorfunction erfc.
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_approxValue
SAL_DLLPUBLIC double rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C()
Rounds value to 15 significant decimal digits.
rtl_math_approxEqual
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 ...
rtl_math_stringToDouble
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.
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_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_math_acosh
SAL_DLLPUBLIC double rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C()
Returns values of the inverse hyperbolic cosine.
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_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.
strbuf.hxx
string.hxx
ustrbuf.hxx
rtl
Definition: bootstrap.hxx:34
rtl::math::approxEqual
bool approxEqual(double a, double b)
A wrapper around rtl_math_approxEqual.
Definition: math.hxx:285
rtl::math::approxAdd
double approxAdd(double a, double b)
Add two values.
Definition: math.hxx:314
rtl::math::isNan
bool isNan(double d)
Test on any QNAN or SNAN.
Definition: math.hxx:376
rtl::math::sin
double sin(double d)
Safe sin(), returns NAN if not valid.
Definition: math.hxx:439
rtl::math::log1p
double log1p(double fValue)
A wrapper around rtl_math_log1p.
Definition: math.hxx:243
rtl::math::erf
double erf(double fValue)
A wrapper around rtl_math_erf.
Definition: math.hxx:257
rtl::math::approxSub
double approxSub(double a, double b)
Subtract two values (a-b).
Definition: math.hxx:327
rtl::math::stringToDouble
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
rtl::math::approxFloor
double approxFloor(double a)
floor() method taking approxValue() into account.
Definition: math.hxx:338
rtl::math::asinh
double asinh(double fValue)
A wrapper around rtl_math_asinh.
Definition: math.hxx:271
rtl::math::isValidArcArg
bool isValidArcArg(double d)
If a value is a valid argument for sin(), cos(), tan().
Definition: math.hxx:429
rtl::math::setNan
void setNan(double *pd)
Set a QNAN.
Definition: math.hxx:408
rtl::math::expm1
double expm1(double fValue)
A wrapper around rtl_math_expm1.
Definition: math.hxx:236
rtl::math::doubleToString
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
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:141
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:109
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:213
rtl::math::isInf
bool isInf(double d)
If a value represents +INF or -INF.
Definition: math.hxx:365
rtl::math::erfc
double erfc(double fValue)
A wrapper around rtl_math_erfc.
Definition: math.hxx:264
rtl::math::approxValue
double approxValue(double fValue)
A wrapper around rtl_math_approxValue.
Definition: math.hxx:229
rtl::math::doubleToStringBuffer
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
rtl::math::atanh
double atanh(double fValue)
A wrapper around rtl_math_atanh.
Definition: math.hxx:250
rtl::math::isSignBitSet
bool isSignBitSet(double d)
If the sign bit is set.
Definition: math.hxx:387
rtl::math::setInf
void setInf(double *pd, bool bNegative)
Set to +INF if bNegative==false or -INF if bNegative==true.
Definition: math.hxx:394
rtl::math::tan
double tan(double d)
Safe tan(), returns NAN if not valid.
Definition: math.hxx:459
rtl::math::acosh
double acosh(double fValue)
A wrapper around rtl_math_acosh.
Definition: math.hxx:278
rtl::math::pow10Exp
double pow10Exp(double fValue, int nExp)
A wrapper around rtl_math_pow10Exp.
Definition: math.hxx:222
rtl::math::isFinite
bool isFinite(double d)
Tests whether a value is neither INF nor NAN.
Definition: math.hxx:354
rtl::math::approxCeil
double approxCeil(double a)
ceil() method taking approxValue() into account.
Definition: math.hxx:347
rtl::math::cos
double cos(double d)
Safe cos(), returns NAN if not valid.
Definition: math.hxx:449
rtl::OStringBuffer
A string buffer implements a mutable sequence of characters.
Definition: strbuf.hxx:70
rtl::OStringBuffer::accessInternals
void accessInternals(rtl_String ***pInternalData, sal_Int32 **pInternalCapacity)
Allows access to the internal data of this OStringBuffer, for effective manipulation.
Definition: strbuf.hxx:1082
rtl::OStringBuffer::getLength
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: strbuf.hxx:370
rtl::OString
This String class provide base functionality for C++ like 8-Bit character array handling.
Definition: string.hxx:181
rtl::OString::getStr
const char * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the characters of this string.
Definition: string.hxx:603
rtl::OString::getLength
sal_Int32 getLength() const
Returns the length of this string.
Definition: string.hxx:577
rtl::OUStringBuffer
A string buffer implements a mutable sequence of characters.
Definition: ustrbuf.hxx:70
rtl::OUStringBuffer::getLength
sal_Int32 getLength() const
Returns the length (character count) of this string buffer.
Definition: ustrbuf.hxx:411
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:1354
rtl::OUString
This String class provides base functionality for C++ like Unicode character array handling.
Definition: ustring.hxx:204
rtl::OUString::getStr
const sal_Unicode * getStr() const SAL_RETURNS_NONNULL
Returns a pointer to the Unicode character buffer for this string.
Definition: ustring.hxx:814
rtl::OUString::getLength
sal_Int32 getLength() const
Returns the length of this string.
Definition: ustring.hxx:792