~ubuntu-branches/ubuntu/karmic/gnustep-base/karmic

« back to all changes in this revision

Viewing changes to Headers/Foundation/NSDecimal.h

  • Committer: Bazaar Package Importer
  • Author(s): Eric Heintzmann
  • Date: 2005-04-17 00:14:38 UTC
  • mfrom: (1.2.1 upstream) (2.1.2 hoary)
  • Revision ID: james.westby@ubuntu.com-20050417001438-enf0y07c9tku85z1
Tags: 1.10.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* NSDecimal types and functions
 
2
   Copyright (C) 1998 Free Software Foundation, Inc.
 
3
 
 
4
   Written by:  Richard Frith-Macdonald <richard@brainstorm.co.uk>
 
5
   Created: November 1998
 
6
 
 
7
   This file is part of the GNUstep Base Library.
 
8
 
 
9
   This library is free software; you can redistribute it and/or
 
10
   modify it under the terms of the GNU Library General Public
 
11
   License as published by the Free Software Foundation; either
 
12
   version 2 of the License, or (at your option) any later version.
 
13
 
 
14
   This library is distributed in the hope that it will be useful,
 
15
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
17
   Library General Public License for more details.
 
18
 
 
19
   You should have received a copy of the GNU Library General Public
 
20
   License along with this library; if not, write to the Free
 
21
   Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111 USA.
 
22
 
 
23
   */
 
24
 
 
25
#ifndef __NSDecimal_h_GNUSTEP_BASE_INCLUDE
 
26
#define __NSDecimal_h_GNUSTEP_BASE_INCLUDE
 
27
 
 
28
#ifndef STRICT_OPENSTEP
 
29
 
 
30
#include <GSConfig.h>
 
31
 
 
32
#if     USE_GMP
 
33
#include <gmp.h>
 
34
#endif
 
35
 
 
36
#include <Foundation/NSObject.h>
 
37
 
 
38
/**
 
39
 *  Enumerated type for specifying decimal rounding behavior.  Can be one of
 
40
 *  <code>NSRoundDown</code> (always round down), <code>NSRoundUp</code>
 
41
 *  (always round up), <code>NSRoundPlain</code> ("normal" rounding (up from
 
42
 *  .5 or above, down otherwise), <code>NSRoundBankers</code> (as "Plain" but
 
43
 *  .5 rounds to make last remaining digit even).  See the
 
44
 *  [(NSDecimalNumberBehaviors)] protocol.
 
45
 */
 
46
typedef enum {
 
47
  NSRoundDown,
 
48
  NSRoundUp,
 
49
  NSRoundPlain,         /* Round .5 up          */
 
50
  NSRoundBankers        /* Make last digit even */
 
51
} NSRoundingMode;
 
52
 
 
53
/**
 
54
 *  Enumerated type for specifying a decimal calculation error.  Can be one of
 
55
 *  the following:
 
56
 *  <deflist>
 
57
 *  <term><code>NSCalculationNoError</code></term>
 
58
 *  <desc>No error occurred.</desc>
 
59
 *  <term><code>NSCalculationLossOfPrecision</code></term>
 
60
 *  <desc>The number can't be represented in 38 significant digits.</desc>
 
61
 *  <term><code>NSCalculationOverflow</code></term>
 
62
 *  <desc>The number is too large to represent.</desc>
 
63
 *  <term><code>NSCalculationUnderflow</code></term>
 
64
 *  <desc>The number is too small to represent.</desc>
 
65
 *  <term><code>NSCalculationDivideByZero</code></term>
 
66
 *  <desc>The caller tried to divide by 0.</desc>
 
67
 *  </deflist>
 
68
 */
 
69
typedef enum {
 
70
  NSCalculationNoError = 0,
 
71
  NSCalculationUnderflow,       /* result became zero */
 
72
  NSCalculationOverflow,
 
73
  NSCalculationLossOfPrecision,
 
74
  NSCalculationDivideByZero
 
75
} NSCalculationError;
 
76
 
 
77
/**
 
78
 *      Give a precision of at least 38 decimal digits
 
79
 *      requires 128 bits.
 
80
 */
 
81
#define NSDecimalMaxSize (16/sizeof(mp_limb_t))
 
82
 
 
83
#define NSDecimalMaxDigit 38
 
84
#define NSDecimalNoScale 128
 
85
 
 
86
/**
 
87
 *  <p>Structure providing equivalent functionality, in conjunction with a set
 
88
 *  of functions, to the [NSDecimalNumber] class.</p>
 
89
<example>
 
90
typedef struct {
 
91
  signed char   exponent;   // Signed exponent - -128 to 127
 
92
  BOOL  isNegative;         // Is this negative?
 
93
  BOOL  validNumber;        // Is this a valid number?
 
94
  unsigned char length;     // digits in mantissa.
 
95
  unsigned char  cMantissa[2*NSDecimalMaxDigit];
 
96
} NSDecimal;
 
97
</example>
 
98
* <p>Instances can be initialized using the NSDecimalFromString(NSString *)
 
99
* function.</p>
 
100
 */
 
101
typedef struct {
 
102
  signed char   exponent;       /* Signed exponent - -128 to 127        */
 
103
  BOOL  isNegative;     /* Is this negative?                    */
 
104
  BOOL  validNumber;    /* Is this a valid number?              */
 
105
#if     USE_GMP
 
106
  mp_size_t size;
 
107
  mp_limb_t lMantissa[NSDecimalMaxSize];
 
108
#else
 
109
  unsigned char length;         /* digits in mantissa.                  */
 
110
  unsigned char cMantissa[NSDecimalMaxDigit];
 
111
#endif
 
112
} NSDecimal;
 
113
 
 
114
/** Returns whether decimal represents an invalid number (i.e., an "NaN" as
 
115
    might result from an overflow or a division by zero). */
 
116
static inline BOOL
 
117
NSDecimalIsNotANumber(const NSDecimal *decimal)
 
118
{
 
119
  return (decimal->validNumber == NO);
 
120
}
 
121
 
 
122
/** Copies value of decimal number to preallocated destination. */
 
123
GS_EXPORT void
 
124
NSDecimalCopy(NSDecimal *destination, const NSDecimal *source);
 
125
 
 
126
/** Tries to reduce memory used to store number internally. */
 
127
GS_EXPORT void
 
128
NSDecimalCompact(NSDecimal *number);
 
129
 
 
130
/**
 
131
 *  Returns <code>NSOrderedDescending</code>, <code>NSOrderedSame</code>, or
 
132
 *  <code>NSOrderedAscending</code> depending on whether leftOperand is
 
133
 *  greater than, equal to, or less than rightOperand.
 
134
 */
 
135
GS_EXPORT NSComparisonResult
 
136
NSDecimalCompare(const NSDecimal *leftOperand, const NSDecimal *rightOperand);
 
137
 
 
138
/**
 
139
 *  Rounds number to result such that it has at most scale digits to the right
 
140
 *  of its decimal point, according to mode (see the
 
141
 *  [(NSDecimalNumberBehaviors)] protocol).  The result should be preallocated
 
142
 *  but can be the same as number.
 
143
 */
 
144
GS_EXPORT void
 
145
NSDecimalRound(NSDecimal *result, const NSDecimal *number, int scale, NSRoundingMode mode);
 
146
 
 
147
/**
 
148
 *  Sets the exponents of n1 and n2 equal to one another, adjusting mantissas
 
149
 *  as necessary to preserve values.  This makes certain operations quicker.
 
150
 */
 
151
GS_EXPORT NSCalculationError
 
152
NSDecimalNormalize(NSDecimal *n1, NSDecimal *n2, NSRoundingMode mode);
 
153
 
 
154
/**
 
155
 *  Adds two decimals and returns result to 38-digit precision.  See the
 
156
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
157
 *  return value.  The result should be preallocated but can be the same as
 
158
 *  left or right.
 
159
 */
 
160
GS_EXPORT NSCalculationError
 
161
NSDecimalAdd(NSDecimal *result, const NSDecimal *left, const NSDecimal *right, NSRoundingMode mode);
 
162
 
 
163
/**
 
164
 *  Subtracts two decimals and returns result to 38-digit precision.  See the
 
165
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
166
 *  return value.  The result should be preallocated but can be the same as
 
167
 *  left or right.
 
168
 */
 
169
GS_EXPORT NSCalculationError
 
170
NSDecimalSubtract(NSDecimal *result, const NSDecimal *left, const NSDecimal *right, NSRoundingMode mode);
 
171
 
 
172
/**
 
173
 *  Multiplies two decimals and returns result to 38-digit precision.  See the
 
174
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
175
 *  return value.  The result should be preallocated but can be the same as
 
176
 *  l or r.
 
177
 */
 
178
GS_EXPORT NSCalculationError
 
179
NSDecimalMultiply(NSDecimal *result, const NSDecimal *l, const NSDecimal *r, NSRoundingMode mode);
 
180
 
 
181
/**
 
182
 *  Divides l by rr and returns result to 38-digit precision.  See the
 
183
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
184
 *  return value.  The result should be preallocated but can be the same as
 
185
 *  l or rr.
 
186
 */
 
187
GS_EXPORT NSCalculationError
 
188
NSDecimalDivide(NSDecimal *result, const NSDecimal *l, const NSDecimal *rr, NSRoundingMode mode);
 
189
    
 
190
/**
 
191
 *  Raises n to power and returns result to 38-digit precision.  See the
 
192
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
193
 *  return value.  The result should be preallocated but can be the same as
 
194
 *  n or power.
 
195
 */
 
196
GS_EXPORT NSCalculationError
 
197
NSDecimalPower(NSDecimal *result, const NSDecimal *n, unsigned power, NSRoundingMode mode);
 
198
 
 
199
/**
 
200
 *  Multiplies n by 10^power and returns result to 38-digit precision.  See the
 
201
 *  [(NSDecimalNumberBehaviors)] protocol for a description of mode and the
 
202
 *  return value.  The result should be preallocated but can be the same as
 
203
 *  n.
 
204
 */
 
205
GS_EXPORT NSCalculationError
 
206
NSDecimalMultiplyByPowerOf10(NSDecimal *result, const NSDecimal *n, short power, NSRoundingMode mode);
 
207
 
 
208
/**
 
209
 *  Returns a string representing the full decimal value, formatted according
 
210
 *  to locale (send nil here for default locale).
 
211
 */
 
212
GS_EXPORT NSString*
 
213
NSDecimalString(const NSDecimal *decimal, NSDictionary *locale);
 
214
 
 
215
 
 
216
// GNUstep extensions to make the implementation of NSDecimalNumber totaly 
 
217
// independent for NSDecimals internal representation
 
218
 
 
219
/** Give back the biggest NSDecimal in (preallocated) result. */
 
220
GS_EXPORT void
 
221
NSDecimalMax(NSDecimal *result);
 
222
 
 
223
/** Give back the smallest NSDecimal in (preallocated) result. */
 
224
GS_EXPORT void
 
225
NSDecimalMin(NSDecimal *result);
 
226
 
 
227
/** Give back the value of a NSDecimal as a double in (preallocated) result. */
 
228
GS_EXPORT double
 
229
NSDecimalDouble(NSDecimal *number);
 
230
 
 
231
/**
 
232
 *  Create a NSDecimal with a mantissa, exponent and a negative flag in
 
233
 *  (preallocated) result.
 
234
 */
 
235
GS_EXPORT void
 
236
NSDecimalFromComponents(NSDecimal *result, unsigned long long mantissa, 
 
237
                      short exponent, BOOL negative);
 
238
 
 
239
/**
 
240
 *  Create a NSDecimal from a string using the locale, in (preallocated)
 
241
 *  result.
 
242
 */
 
243
GS_EXPORT void
 
244
NSDecimalFromString(NSDecimal *result, NSString *numberValue, 
 
245
                    NSDictionary *locale);
 
246
 
 
247
#endif
 
248
#endif
 
249