My Project
UDK 3.2.7 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  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * Copyright 2000, 2010 Oracle and/or its affiliates.
7  *
8  * OpenOffice.org - a multi-platform office productivity suite
9  *
10  * This file is part of OpenOffice.org.
11  *
12  * OpenOffice.org is free software: you can redistribute it and/or modify
13  * it under the terms of the GNU Lesser General Public License version 3
14  * only, as published by the Free Software Foundation.
15  *
16  * OpenOffice.org is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License version 3 for more details
20  * (a copy is included in the LICENSE file that accompanied this code).
21  *
22  * You should have received a copy of the GNU Lesser General Public License
23  * version 3 along with OpenOffice.org. If not, see
24  * <http://www.openoffice.org/license.html>
25  * for a copy of the LGPLv3 License.
26  *
27  ************************************************************************/
28 
29 #if !defined INCLUDED_RTL_MATH_HXX
30 #define INCLUDED_RTL_MATH_HXX
31 
32 #include "rtl/math.h"
33 #include "rtl/string.hxx"
34 #include "rtl/ustring.hxx"
35 #include "rtl/ustrbuf.hxx"
36 #include "sal/mathconf.h"
37 #include "sal/types.h"
38 
39 #include <math.h>
40 
41 namespace rtl {
42 
43 namespace math {
44 
47 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
48  sal_Int32 nDecPlaces,
49  sal_Char cDecSeparator,
50  sal_Int32 const * pGroups,
51  sal_Char cGroupSeparator,
52  bool bEraseTrailingDecZeros = false)
53 {
54  rtl::OString aResult;
55  rtl_math_doubleToString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
56  cDecSeparator, pGroups, cGroupSeparator,
57  bEraseTrailingDecZeros);
58  return aResult;
59 }
60 
63 inline rtl::OString doubleToString(double fValue, rtl_math_StringFormat eFormat,
64  sal_Int32 nDecPlaces,
65  sal_Char cDecSeparator,
66  bool bEraseTrailingDecZeros = false)
67 {
68  rtl::OString aResult;
69  rtl_math_doubleToString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
70  cDecSeparator, 0, 0, bEraseTrailingDecZeros);
71  return aResult;
72 }
73 
76 inline rtl::OUString doubleToUString(double fValue,
77  rtl_math_StringFormat eFormat,
78  sal_Int32 nDecPlaces,
79  sal_Unicode cDecSeparator,
80  sal_Int32 const * pGroups,
81  sal_Unicode cGroupSeparator,
82  bool bEraseTrailingDecZeros = false)
83 {
84  rtl::OUString aResult;
85  rtl_math_doubleToUString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
86  cDecSeparator, pGroups, cGroupSeparator,
87  bEraseTrailingDecZeros);
88  return aResult;
89 }
90 
93 inline rtl::OUString doubleToUString(double fValue,
94  rtl_math_StringFormat eFormat,
95  sal_Int32 nDecPlaces,
96  sal_Unicode cDecSeparator,
97  bool bEraseTrailingDecZeros = false)
98 {
99  rtl::OUString aResult;
100  rtl_math_doubleToUString(&aResult.pData, 0, 0, fValue, eFormat, nDecPlaces,
101  cDecSeparator, 0, 0, bEraseTrailingDecZeros);
102  return aResult;
103 }
104 
108 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
109  rtl_math_StringFormat eFormat,
110  sal_Int32 nDecPlaces,
111  sal_Unicode cDecSeparator,
112  sal_Int32 const * pGroups,
113  sal_Unicode cGroupSeparator,
114  bool bEraseTrailingDecZeros = false)
115 {
116  rtl_uString ** pData;
117  sal_Int32 * pCapacity;
118  rBuffer.accessInternals( &pData, &pCapacity );
119  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
120  eFormat, nDecPlaces, cDecSeparator, pGroups,
121  cGroupSeparator, bEraseTrailingDecZeros);
122 }
123 
127 inline void doubleToUStringBuffer( rtl::OUStringBuffer& rBuffer, double fValue,
128  rtl_math_StringFormat eFormat,
129  sal_Int32 nDecPlaces,
130  sal_Unicode cDecSeparator,
131  bool bEraseTrailingDecZeros = false)
132 {
133  rtl_uString ** pData;
134  sal_Int32 * pCapacity;
135  rBuffer.accessInternals( &pData, &pCapacity );
136  rtl_math_doubleToUString( pData, pCapacity, rBuffer.getLength(), fValue,
137  eFormat, nDecPlaces, cDecSeparator, 0, 0,
138  bEraseTrailingDecZeros);
139 }
140 
143 inline double stringToDouble(rtl::OString const & rString,
144  sal_Char cDecSeparator, sal_Char cGroupSeparator,
145  rtl_math_ConversionStatus * pStatus = 0,
146  sal_Int32 * pParsedEnd = 0)
147 {
148  sal_Char const * pBegin = rString.getStr();
149  sal_Char const * pEnd;
150  double fResult = rtl_math_stringToDouble(pBegin,
151  pBegin + rString.getLength(),
152  cDecSeparator, cGroupSeparator,
153  pStatus, &pEnd);
154  if (pParsedEnd != 0)
155  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
156  return fResult;
157 }
158 
161 inline double stringToDouble(rtl::OUString const & rString,
162  sal_Unicode cDecSeparator,
163  sal_Unicode cGroupSeparator,
164  rtl_math_ConversionStatus * pStatus = 0,
165  sal_Int32 * pParsedEnd = 0)
166 {
167  sal_Unicode const * pBegin = rString.getStr();
168  sal_Unicode const * pEnd;
169  double fResult = rtl_math_uStringToDouble(pBegin,
170  pBegin + rString.getLength(),
171  cDecSeparator, cGroupSeparator,
172  pStatus, &pEnd);
173  if (pParsedEnd != 0)
174  *pParsedEnd = (sal_Int32)(pEnd - pBegin);
175  return fResult;
176 }
177 
180 inline double round(
181  double fValue, int nDecPlaces = 0,
183 {
184  return rtl_math_round(fValue, nDecPlaces, eMode);
185 }
186 
189 inline double pow10Exp(double fValue, int nExp)
190 {
191  return rtl_math_pow10Exp(fValue, nExp);
192 }
193 
196 inline double approxValue(double fValue)
197 {
198  return rtl_math_approxValue(fValue);
199 }
200 
203 inline double expm1(double fValue)
204 {
205  return rtl_math_expm1(fValue);
206 }
207 
210 inline double log1p(double fValue)
211 {
212  return rtl_math_log1p(fValue);
213 }
214 
217 inline double atanh(double fValue)
218 {
219  return rtl_math_atanh(fValue);
220 }
221 
224 inline double erf(double fValue)
225 {
226  return rtl_math_erf(fValue);
227 }
228 
231 inline double erfc(double fValue)
232 {
233  return rtl_math_erfc(fValue);
234 }
235 
238 inline double asinh(double fValue)
239 {
240  return rtl_math_asinh(fValue);
241 }
242 
245 inline double acosh(double fValue)
246 {
247  return rtl_math_acosh(fValue);
248 }
249 
250 
257 inline bool approxEqual(double a, double b)
258 {
259  if ( a == b )
260  return true;
261  double x = a - b;
262  return (x < 0.0 ? -x : x)
263  < ((a < 0.0 ? -a : a) * (1.0 / (16777216.0 * 16777216.0)));
264 }
265 
271 inline bool approxEqual(double a, double b, sal_Int16 nPrec)
272 {
273  if ( a == b )
274  return true;
275  double x = a - b;
276  return (x < 0.0 ? -x : x)
277  < ((a < 0.0 ? -a : a) * (1.0 / (pow(static_cast<double>(2.0), nPrec))));
278 }
289 inline double approxAdd(double a, double b)
290 {
291  if ( ((a < 0.0 && b > 0.0) || (b < 0.0 && a > 0.0))
292  && approxEqual( a, -b ) )
293  return 0.0;
294  return a + b;
295 }
296 
302 inline double approxSub(double a, double b)
303 {
304  if ( ((a < 0.0 && b < 0.0) || (a > 0.0 && b > 0.0)) && approxEqual( a, b ) )
305  return 0.0;
306  return a - b;
307 }
308 
313 inline double approxFloor(double a)
314 {
315  return floor( approxValue( a ));
316 }
317 
322 inline double approxCeil(double a)
323 {
324  return ceil( approxValue( a ));
325 }
326 
329 inline bool isFinite(double d)
330 {
331  return SAL_MATH_FINITE(d) != 0;
332 }
333 
340 inline bool isInf(double d)
341 {
342  // exponent==0x7ff fraction==0
343  return (SAL_MATH_FINITE(d) == 0) &&
344  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi == 0)
345  && (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
346  == 0);
347 }
348 
351 inline bool isNan(double d)
352 {
353  // exponent==0x7ff fraction!=0
354  return (SAL_MATH_FINITE(d) == 0) && (
355  (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_hi != 0)
356  || (reinterpret_cast< sal_math_Double * >(&d)->inf_parts.fraction_lo
357  != 0) );
358 }
359 
362 inline bool isSignBitSet(double d)
363 {
364  return reinterpret_cast< sal_math_Double * >(&d)->inf_parts.sign != 0;
365 }
366 
369 inline void setInf(double * pd, bool bNegative)
370 {
371  union
372  {
373  double sd;
374  sal_math_Double md;
375  };
376  md.w32_parts.msw = bNegative ? 0xFFF00000 : 0x7FF00000;
377  md.w32_parts.lsw = 0;
378  *pd = sd;
379 }
380 
383 inline void setNan(double * pd)
384 {
385  union
386  {
387  double sd;
388  sal_math_Double md;
389  };
390  md.w32_parts.msw = 0x7FFFFFFF;
391  md.w32_parts.lsw = 0xFFFFFFFF;
392  *pd = sd;
393 }
394 
404 inline bool isValidArcArg(double d)
405 {
406  return fabs(d)
407  <= (static_cast< double >(static_cast< unsigned long >(0x80000000))
408  * static_cast< double >(static_cast< unsigned long >(0x80000000))
409  * 2);
410 }
411 
414 inline double sin(double d)
415 {
416  if ( isValidArcArg( d ) )
417  return ::sin( d );
418  setNan( &d );
419  return d;
420 }
421 
424 inline double cos(double d)
425 {
426  if ( isValidArcArg( d ) )
427  return ::cos( d );
428  setNan( &d );
429  return d;
430 }
431 
434 inline double tan(double d)
435 {
436  if ( isValidArcArg( d ) )
437  return ::tan( d );
438  setNan( &d );
439  return d;
440 }
441 
442 }
443 
444 }
445 
446 #endif // INCLUDED_RTL_MATH_HXX
447 
448 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */