]>
Commit | Line | Data |
---|---|---|
b75a7d8f A |
1 | /* |
2 | ****************************************************************************** | |
3 | * | |
374ca955 | 4 | * Copyright (C) 1997-2004, International Business Machines |
b75a7d8f A |
5 | * Corporation and others. All Rights Reserved. |
6 | * | |
7 | ****************************************************************************** | |
8 | * | |
9 | * File DIGITLST.H | |
10 | * | |
11 | * Modification History: | |
12 | * | |
13 | * Date Name Description | |
14 | * 02/25/97 aliu Converted from java. | |
15 | * 03/21/97 clhuang Updated per C++ implementation. | |
16 | * 04/15/97 aliu Changed MAX_COUNT to DBL_DIG. Changed Digit to char. | |
17 | * 09/09/97 aliu Adapted for exponential notation support. | |
18 | * 08/02/98 stephen Added nearest/even rounding | |
19 | * 06/29/99 stephen Made LONG_DIGITS a macro to satisfy SUN compiler | |
20 | * 07/09/99 stephen Removed kMaxCount (unused, for HP compiler) | |
21 | ****************************************************************************** | |
22 | */ | |
23 | ||
24 | #ifndef DIGITLST_H | |
25 | #define DIGITLST_H | |
26 | ||
27 | #include "unicode/utypes.h" | |
28 | #include "unicode/uobject.h" | |
29 | #include <float.h> | |
30 | ||
374ca955 | 31 | // Decimal digits in a 64-bit int |
b75a7d8f | 32 | //#define LONG_DIGITS 19 |
374ca955 | 33 | #define INT64_DIGITS 19 |
b75a7d8f A |
34 | |
35 | typedef enum EDigitListValues { | |
374ca955 A |
36 | MAX_DBL_DIGITS = DBL_DIG, |
37 | MAX_I64_DIGITS = INT64_DIGITS, | |
38 | MAX_DIGITS = MAX_I64_DIGITS, | |
b75a7d8f A |
39 | MAX_EXPONENT = DBL_DIG, |
40 | DIGIT_PADDING = 3, | |
41 | ||
42 | // "+." + fDigits + "e" + fDecimalAt | |
374ca955 | 43 | MAX_DEC_DIGITS = MAX_DIGITS + DIGIT_PADDING + MAX_EXPONENT |
b75a7d8f A |
44 | } EDigitListValues; |
45 | ||
46 | U_NAMESPACE_BEGIN | |
47 | ||
48 | /** | |
49 | * Digit List utility class. Private to DecimalFormat. Handles the transcoding | |
50 | * between numeric values and strings of characters. Only handles | |
51 | * non-negative numbers. The division of labor between DigitList and | |
52 | * DecimalFormat is that DigitList handles the radix 10 representation | |
53 | * issues; DecimalFormat handles the locale-specific issues such as | |
54 | * positive/negative, grouping, decimal point, currency, and so on. | |
55 | * <P> | |
56 | * A DigitList is really a representation of a floating point value. | |
57 | * It may be an integer value; we assume that a double has sufficient | |
58 | * precision to represent all digits of a long. | |
59 | * <P> | |
60 | * The DigitList representation consists of a string of characters, | |
61 | * which are the digits radix 10, from '0' to '9'. It also has a radix | |
62 | * 10 exponent associated with it. The value represented by a DigitList | |
63 | * object can be computed by mulitplying the fraction f, where 0 <= f < 1, | |
64 | * derived by placing all the digits of the list to the right of the | |
65 | * decimal point, by 10^exponent. | |
66 | */ | |
374ca955 | 67 | class U_I18N_API DigitList : public UMemory { // Declare external to make compiler happy |
b75a7d8f A |
68 | public: |
69 | DigitList(); | |
70 | ~DigitList(); | |
71 | ||
72 | /* copy constructor | |
73 | * @param DigitList The object to be copied. | |
74 | * @return the newly created object. | |
75 | */ | |
76 | DigitList(const DigitList&); // copy constructor | |
77 | ||
78 | /* assignment operator | |
79 | * @param DigitList The object to be copied. | |
80 | * @return the newly created object. | |
81 | */ | |
82 | DigitList& operator=(const DigitList&); // assignment operator | |
83 | ||
84 | /** | |
85 | * Return true if another object is semantically equal to this one. | |
86 | * @param other The DigitList to be compared for equality | |
87 | * @return true if another object is semantically equal to this one. | |
88 | * return false otherwise. | |
89 | */ | |
90 | UBool operator==(const DigitList& other) const; | |
91 | ||
92 | /** | |
93 | * Return true if another object is semantically unequal to this one. | |
94 | * @param other The DigitList to be compared for inequality | |
95 | * @return true if another object is semantically unequal to this one. | |
96 | * return false otherwise. | |
97 | */ | |
98 | UBool operator!=(const DigitList& other) const { return !operator==(other); } | |
99 | ||
100 | /** | |
101 | * Clears out the digits. | |
102 | * Use before appending them. | |
103 | * Typically, you set a series of digits with append, then at the point | |
104 | * you hit the decimal point, you set myDigitList.fDecimalAt = myDigitList.fCount; | |
105 | * then go on appending digits. | |
106 | */ | |
107 | void clear(void); | |
108 | ||
109 | /** | |
110 | * Appends digits to the list. Ignores all digits beyond the first DBL_DIG, | |
111 | * since they are not significant for either longs or doubles. | |
112 | * @param digit The digit to be appended. | |
113 | */ | |
114 | inline void append(char digit); | |
115 | ||
116 | /** | |
117 | * Utility routine to get the value of the digit list | |
118 | * Returns 0.0 if zero length. | |
119 | * @return the value of the digit list. | |
120 | */ | |
374ca955 | 121 | double getDouble(void) /*const*/; |
b75a7d8f A |
122 | |
123 | /** | |
124 | * Utility routine to get the value of the digit list | |
125 | * Make sure that fitsIntoLong() is called before calling this function. | |
126 | * Returns 0 if zero length. | |
127 | * @return the value of the digit list, return 0 if it is zero length | |
128 | */ | |
374ca955 A |
129 | int32_t getLong(void) /*const*/; |
130 | ||
131 | /** | |
132 | * Utility routine to get the value of the digit list | |
133 | * Make sure that fitsIntoInt64() is called before calling this function. | |
134 | * Returns 0 if zero length. | |
135 | * @return the value of the digit list, return 0 if it is zero length | |
136 | */ | |
137 | int64_t getInt64(void) /*const*/; | |
b75a7d8f A |
138 | |
139 | /** | |
140 | * Return true if the number represented by this object can fit into | |
141 | * a long. | |
142 | * @param ignoreNegativeZero True if negative zero is ignored. | |
143 | * @return true if the number represented by this object can fit into | |
144 | * a long, return false otherwise. | |
145 | */ | |
374ca955 A |
146 | UBool fitsIntoLong(UBool ignoreNegativeZero) /*const*/; |
147 | ||
148 | /** | |
149 | * Return true if the number represented by this object can fit into | |
150 | * an int64_t. | |
151 | * @param ignoreNegativeZero True if negative zero is ignored. | |
152 | * @return true if the number represented by this object can fit into | |
153 | * a long, return false otherwise. | |
154 | */ | |
155 | UBool fitsIntoInt64(UBool ignoreNegativeZero) /*const*/; | |
b75a7d8f A |
156 | |
157 | /** | |
158 | * Utility routine to set the value of the digit list from a double | |
159 | * Input must be non-negative, and must not be Inf, -Inf, or NaN. | |
160 | * The maximum fraction digits helps us round properly. | |
161 | * @param source The value to be set | |
162 | * @param maximunDigits The maximum number of digits to be shown | |
163 | * @param fixedPoint True if the point is fixed | |
164 | */ | |
165 | void set(double source, int32_t maximumDigits, UBool fixedPoint = TRUE); | |
166 | ||
167 | /** | |
168 | * Utility routine to set the value of the digit list from a long. | |
169 | * If a non-zero maximumDigits is specified, no more than that number of | |
170 | * significant digits will be produced. | |
171 | * @param source The value to be set | |
172 | * @param maximunDigits The maximum number of digits to be shown | |
173 | */ | |
174 | void set(int32_t source, int32_t maximumDigits = 0); | |
175 | ||
374ca955 A |
176 | /** |
177 | * Utility routine to set the value of the digit list from an int64. | |
178 | * If a non-zero maximumDigits is specified, no more than that number of | |
179 | * significant digits will be produced. | |
180 | * @param source The value to be set | |
181 | * @param maximunDigits The maximum number of digits to be shown | |
182 | */ | |
183 | void set(int64_t source, int32_t maximumDigits = 0); | |
184 | ||
b75a7d8f A |
185 | /** |
186 | * Return true if this is a representation of zero. | |
187 | * @return true if this is a representation of zero. | |
188 | */ | |
189 | UBool isZero(void) const; | |
190 | ||
191 | /** | |
192 | * Return true if this is a representation of LONG_MIN. You must use | |
193 | * this method to determine if this is so; you cannot check directly, | |
194 | * because a special format is used to handle this. | |
195 | */ | |
196 | // This code is unused. | |
197 | //UBool isLONG_MIN(void) const; | |
198 | ||
199 | public: | |
200 | /** | |
201 | * These data members are intentionally public and can be set directly. | |
202 | *<P> | |
203 | * The value represented is given by placing the decimal point before | |
204 | * fDigits[fDecimalAt]. If fDecimalAt is < 0, then leading zeros between | |
205 | * the decimal point and the first nonzero digit are implied. If fDecimalAt | |
206 | * is > fCount, then trailing zeros between the fDigits[fCount-1] and the | |
207 | * decimal point are implied. | |
208 | * <P> | |
209 | * Equivalently, the represented value is given by f * 10^fDecimalAt. Here | |
210 | * f is a value 0.1 <= f < 1 arrived at by placing the digits in fDigits to | |
211 | * the right of the decimal. | |
212 | * <P> | |
213 | * DigitList is normalized, so if it is non-zero, fDigits[0] is non-zero. We | |
214 | * don't allow denormalized numbers because our exponent is effectively of | |
215 | * unlimited magnitude. The fCount value contains the number of significant | |
216 | * digits present in fDigits[]. | |
217 | * <P> | |
218 | * Zero is represented by any DigitList with fCount == 0 or with each fDigits[i] | |
219 | * for all i <= fCount == '0'. | |
220 | */ | |
221 | int32_t fDecimalAt; | |
222 | int32_t fCount; | |
223 | UBool fIsPositive; | |
224 | char *fDigits; | |
225 | ||
226 | private: | |
227 | ||
228 | /* One character before fDigits for the decimal*/ | |
229 | char fDecimalDigits[MAX_DEC_DIGITS + 1]; | |
230 | ||
231 | /** | |
232 | * Round the representation to the given number of digits. | |
233 | * @param maximumDigits The maximum number of digits to be shown. | |
234 | * Upon return, count will be less than or equal to maximumDigits. | |
235 | */ | |
236 | void round(int32_t maximumDigits); | |
237 | ||
374ca955 | 238 | UBool shouldRoundUp(int32_t maximumDigits) const; |
b75a7d8f A |
239 | }; |
240 | ||
241 | // ------------------------------------- | |
242 | // Appends the digit to the digit list if it's not out of scope. | |
243 | // Ignores the digit, otherwise. | |
244 | ||
245 | inline void | |
246 | DigitList::append(char digit) | |
247 | { | |
248 | // Ignore digits which exceed the precision we can represent | |
249 | if (fCount < MAX_DIGITS) | |
250 | fDigits[fCount++] = digit; | |
251 | } | |
252 | ||
253 | U_NAMESPACE_END | |
254 | #endif // _DIGITLST | |
255 | ||
256 | //eof |