summaryrefslogtreecommitdiff
path: root/sal/inc/rtl/math.h
blob: 46efef7b17892368a408263b52794b7d45ace1e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
/*************************************************************************
 *
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 * 
 * Copyright 2000, 2010 Oracle and/or its affiliates.
 *
 * OpenOffice.org - a multi-platform office productivity suite
 *
 * This file is part of OpenOffice.org.
 *
 * OpenOffice.org is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License version 3
 * only, as published by the Free Software Foundation.
 *
 * OpenOffice.org is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License version 3 for more details
 * (a copy is included in the LICENSE file that accompanied this code).
 *
 * You should have received a copy of the GNU Lesser General Public License
 * version 3 along with OpenOffice.org.  If not, see
 * <http://www.openoffice.org/license.html>
 * for a copy of the LGPLv3 License.
 *
 ************************************************************************/

#if !defined INCLUDED_RTL_MATH_H
#define INCLUDED_RTL_MATH_H

#include "rtl/ustring.h"
#include "sal/types.h"

#if defined __cplusplus
extern "C" {
#endif /* __cplusplus */

/** Formatting modes for rtl_math_doubleToString and rtl_math_doubleToUString
    and rtl_math_doubleToUStringBuffer.
 */
enum rtl_math_StringFormat
{
    /** Like sprintf() %E.
     */
    rtl_math_StringFormat_E,

    /** Like sprintf() %f.
     */
    rtl_math_StringFormat_F,

    /** Like sprintf() %G, 'F' or 'E' format is used depending on which one is
        more compact.
    */
    rtl_math_StringFormat_G,

    /** Automatic, 'F' or 'E' format is used depending on the numeric value to
        be formatted.
     */
    rtl_math_StringFormat_Automatic,

    /** @internal
     */
    rtl_math_StringFormat_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
};

/** Status for rtl_math_stringToDouble and rtl_math_uStringToDouble.
 */
enum rtl_math_ConversionStatus
{
    /** Conversion was successful.
     */
    rtl_math_ConversionStatus_Ok,

    /** Conversion caused overflow or underflow.
     */
    rtl_math_ConversionStatus_OutOfRange,

    /** @internal
     */
    rtl_math_ConversionStatus_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
};

/** Rounding modes for rtl_math_round.
 */
enum rtl_math_RoundingMode
{
    /** Like HalfUp, but corrects roundoff errors, preferred.
     */
    rtl_math_RoundingMode_Corrected,

    /** Floor of absolute value, signed return (commercial).
     */
    rtl_math_RoundingMode_Down,

    /** Ceil of absolute value, signed return (commercial).
     */
    rtl_math_RoundingMode_Up,

    /** Floor of signed value.
     */
    rtl_math_RoundingMode_Floor,

    /** Ceil of signed value.
     */
    rtl_math_RoundingMode_Ceiling,

    /** Frac <= 0.5 ? floor of abs : ceil of abs, signed return.
     */
    rtl_math_RoundingMode_HalfDown,

    /** Frac < 0.5 ? floor of abs : ceil of abs, signed return (mathematical).
     */
    rtl_math_RoundingMode_HalfUp,

    /** IEEE rounding mode (statistical).
     */
    rtl_math_RoundingMode_HalfEven,

    /** @internal
     */
    rtl_math_RoundingMode_FORCE_EQUAL_SIZE = SAL_MAX_ENUM
};

/** Special decimal places constants for rtl_math_doubleToString and
    rtl_math_doubleToUString and rtl_math_doubleToUStringBuffer.
 */
enum rtl_math_DecimalPlaces
{
    /** Value to be used with rtl_math_StringFormat_Automatic.
     */
    rtl_math_DecimalPlaces_Max = 0x7ffffff,

    /** Value to be used with rtl_math_StringFormat_G.
        In fact the same value as rtl_math_DecimalPlaces_Max, just an alias for
        better understanding.
     */
    rtl_math_DecimalPlaces_DefaultSignificance = 0x7ffffff
};


/** Conversions analogous to sprintf() using internal rounding.

    +/-HUGE_VAL are converted to "INF" and "-INF", NAN values are
    converted to "NaN".

    @param pResult
    Returns the resulting byte string.  Must itself not be null, and must point
    to either null or a valid string.

    @param pResultCapacity
    If null, pResult is considered to point to immutable strings, and a new
    string will be allocated in pResult.
    If non-null, it points to the current capacity of pResult, which is
    considered to point to a string buffer (pResult must not itself be null in
    this case, and must point to a string that has room for the given capacity).
    The string representation of the given double value is inserted into pResult
    at position nResultOffset.  If pResult's current capacity is too small, a
    new string buffer will be allocated in pResult as necessary, and
    pResultCapacity will contain the new capacity on return.

    @param nResultOffset
    If pResult is used as a string buffer (i.e., pResultCapacity is non-null),
    nResultOffset specifies the insertion offset within the buffer.  Ignored
    otherwise.

    @param fValue
    The value to convert.

    @param eFormat
    The format to use, one of rtl_math_StringFormat.

    @param nDecPlaces
    The number of decimals to be generated.  Effectively fValue is rounded at
    this position, specifying nDecPlaces <= 0 accordingly rounds the value
    before the decimal point and fills with zeros.
    If eFormat == rtl_math_StringFormat_Automatic and nDecPlaces ==
    rtl_math_DecimalPlaces_Max, the highest number of significant decimals
    possible is generated.
    If eFormat == rtl_math_StringFormat_G, nDecPlaces specifies the number of
    significant digits instead.  If nDecPlaces ==
    rtl_math_DecimalPlaces_DefaultSignificance, the default number (currently 6
    as implemented by most libraries) of significant digits is generated.
    According to the ANSI C90 standard the E style will be used only if the
    exponent resulting from the conversion is less than -4 or greater than or
    equal to the precision.  However, as opposed to the ANSI standard, trailing
    zeros are not necessarily removed from the fractional portion of the result
    unless bEraseTrailingDecZeros == true was specified.

    @param cDecSeparator
    The decimal separator.

    @param pGroups
    Either null (no grouping is used), or a null-terminated list of group
    lengths.  Each group length must be strictly positive.  If the number of
    digits in a conversion exceeds the specified range, the last (highest) group
    length is repeated as needed.  Values are applied from right to left, for a
    grouping of 1,00,00,000 you'd have to specify pGroups={3,2,0}.

    @param cGroupSeparator
    The group separator.  Ignored if pGroups is null.

    @param bEraseTrailingDecZeros
    Trailing zeros in decimal places are erased.
 */
void SAL_CALL 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.

    +/-HUGE_VAL are converted to "INF" and "-INF", NAN values are
    converted to "NaN".

    @param pResult
    Returns the resulting Unicode string.  Must itself not be null, and must
    point to either null or a valid string.

    @param pResultCapacity
    If null, pResult is considered to point to immutable strings, and a new
    string will be allocated in pResult.
    If non-null, it points to the current capacity of pResult, which is
    considered to point to a string buffer (pResult must not itself be null in
    this case, and must point to a string that has room for the given capacity).
    The string representation of the given double value is inserted into pResult
    at position nResultOffset.  If pResult's current capacity is too small, a
    new string buffer will be allocated in pResult as necessary, and
    pResultCapacity will contain the new capacity on return.

    @param nResultOffset
    If pResult is used as a string buffer (i.e., pResultCapacity is non-null),
    nResultOffset specifies the insertion offset within the buffer.  Ignored
    otherwise.

    @param fValue
    The value to convert.

    @param eFormat
    The format to use, one of rtl_math_StringFormat.

    @param nDecPlaces
    The number of decimals to be generated.  Effectively fValue is rounded at
    this position, specifying nDecPlaces <= 0 accordingly rounds the value
    before the decimal point and fills with zeros.
    If eFormat == rtl_math_StringFormat_Automatic and nDecPlaces ==
    rtl_math_DecimalPlaces_Max, the highest number of significant decimals
    possible is generated.
    If eFormat == rtl_math_StringFormat_G, nDecPlaces specifies the number of
    significant digits instead.  If nDecPlaces ==
    rtl_math_DecimalPlaces_DefaultSignificance, the default number (currently 6
    as implemented by most libraries) of significant digits is generated.
    According to the ANSI C90 standard the E style will be used only if the
    exponent resulting from the conversion is less than -4 or greater than or
    equal to the precision.  However, as opposed to the ANSI standard, trailing
    zeros are not necessarily removed from the fractional portion of the result
    unless bEraseTrailingDecZeros == true was specified.

    @param cDecSeparator
    The decimal separator.

    @param pGroups
    Either null (no grouping is used), or a null-terminated list of group
    lengths.  Each group length must be strictly positive.  If the number of
    digits in a conversion exceeds the specified range, the last (highest) group
    length is repeated as needed.  Values are applied from right to left, for a
    grouping of 1,00,00,000 you'd have to specify pGroups={3,2,0}.

    @param cGroupSeparator
    The group separator.  Ignored if pGroups is null.

    @param bEraseTrailingDecZeros
    Trailing zeros in decimal places are erased.
 */
void SAL_CALL 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();

/** Conversion analogous to strtod(), convert a string representing a
    decimal number into a double value.

    Leading tabs (0x09) and spaces (0x20) are eaten.  Overflow returns
    +/-HUGE_VAL, underflow 0.  In both cases pStatus is set to
    rtl_math_ConversionStatus_OutOfRange, otherwise to
    rtl_math_ConversionStatus_Ok.  "INF", "-INF" and "+/-1.#INF" are
    recognized as +/-HUGE_VAL, pStatus is set to
    rtl_math_ConversionStatus_OutOfRange.  "NaN" and "+/-1.#NAN" are
    recognized and the value is set to +/-NAN, pStatus is set to
    rtl_math_ConversionStatus_Ok.

    @param pBegin
    Points to the start of the byte string to convert.  Must not be null.

    @param pEnd
    Points one past the end of the byte string to convert.  The condition
    pEnd >= pBegin must hold.

    @param cDecSeparator
    The decimal separator.

    @param cGroupSeparator
    The group (aka thousands) separator.

    @param pStatus
    If non-null, returns the status of the conversion.

    @param pParsedEnd
    If non-null, returns one past the position of the last character parsed
    away.  Thus if [pBegin..pEnd) only contains the numerical string to be
    parsed, *pParsedEnd == pEnd on return.
 */
double SAL_CALL 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.

    Leading tabs (U+0009) and spaces (U+0020) are eaten.  Overflow returns
    +/-HUGE_VAL, underflow 0.  In both cases pStatus is set to
    rtl_math_ConversionStatus_OutOfRange, otherwise to
    rtl_math_ConversionStatus_Ok.  "INF", "-INF" and "+/-1.#INF" are
    recognized as +/-HUGE_VAL, pStatus is set to
    rtl_math_ConversionStatus_OutOfRange.  "NaN" and "+/-1.#NAN" are
    recognized and the value is set to +/-NAN, pStatus is set to
    rtl_math_ConversionStatus_Ok.

    @param pBegin
    Points to the start of the Unicode string to convert.  Must not be null.

    @param pEnd
    Points one past the end of the Unicode string to convert.  The condition
    pEnd >= pBegin must hold.

    @param cDecSeparator
    The decimal separator.

    @param cGroupSeparator
    The group (aka thousands) separator.

    @param pStatus
    If non-null, returns the status of the conversion.

    @param pParsedEnd
    If non-null, returns one past the position of the last character parsed
    away.  Thus if [pBegin..pEnd) only contains the numerical string to be
    parsed, *pParsedEnd == pEnd on return.
 */
double SAL_CALL 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();

/** Rounds a double value.

    @param fValue
    Specifies the value to be rounded.

    @param nDecPlaces
    Specifies the decimal place where rounding occurs.  Must be in the range
    -20 to +20, inclusive.  Negative if rounding occurs before the decimal
    point.

    @param eMode
    Specifies the rounding mode.
 */
double SAL_CALL rtl_math_round(double fValue, int nDecPlaces,
                               enum rtl_math_RoundingMode eMode)
    SAL_THROW_EXTERN_C();

/** Scales fVal to a power of 10 without calling pow() or div() for nExp values
    between -16 and +16, providing a faster method.

    @param fValue
    The value to be raised.

    @param nExp
    The exponent.

    @return
    fVal * pow(10.0, nExp)
 */
double SAL_CALL rtl_math_pow10Exp(double fValue, int nExp) SAL_THROW_EXTERN_C();

/** Rounds value to 15 significant decimal digits.

    @param fValue
    The value to be rounded.
  */
double SAL_CALL rtl_math_approxValue(double fValue) SAL_THROW_EXTERN_C();

/** Returns more accurate e^x-1 for x near 0 than calculating directly.

    expm1 is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term e^x-1.
  */
double SAL_CALL rtl_math_expm1(double fValue) SAL_THROW_EXTERN_C();

/** Returns more accurate log(1+x) for x near 0 than calculating directly.

    log1p is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term log(1+x).
  */
double SAL_CALL rtl_math_log1p(double fValue) SAL_THROW_EXTERN_C();

/** Returns more accurate atanh(x) for x near 0 than calculating
    0.5*log((1+x)/(1-x)).

    atanh is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term atanh(x).
  */
double SAL_CALL rtl_math_atanh(double fValue) SAL_THROW_EXTERN_C();

/** Returns values of the Errorfunction erf.

    erf is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term erf(x).
  */
double SAL_CALL rtl_math_erf(double fValue) SAL_THROW_EXTERN_C();

/** Returns values of the complement Errorfunction erfc.

    erfc is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term erfc(x).
  */
double SAL_CALL rtl_math_erfc(double fValue) SAL_THROW_EXTERN_C();

/** Returns values of the inverse hyperbolic sine.

    asinh is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term asinh(x).
  */
double SAL_CALL rtl_math_asinh(double fValue) SAL_THROW_EXTERN_C();

/** Returns values of the inverse hyperbolic cosine.

    acosh is part of the C99 standard, but not provided by some compilers.

    @param fValue
    The value x in the term acosh(x).
  */
double SAL_CALL rtl_math_acosh(double fValue) SAL_THROW_EXTERN_C();

#if defined __cplusplus
}
#endif /* __cplusplus */

#endif /* INCLUDED_RTL_MATH_H */