]>
git.saurik.com Git - apple/icu.git/blob - icuSources/i18n/digitlst.cpp
2 **********************************************************************
3 * Copyright (C) 1997-2004, International Business Machines
4 * Corporation and others. All Rights Reserved.
5 **********************************************************************
9 * Modification History:
11 * Date Name Description
12 * 03/21/97 clhuang Converted from java.
13 * 03/21/97 clhuang Implemented with new APIs.
14 * 03/27/97 helena Updated to pass the simple test after code review.
15 * 03/31/97 aliu Moved isLONG_MIN to here, and fixed it.
16 * 04/15/97 aliu Changed MAX_COUNT to DBL_DIG. Changed Digit to char.
17 * Reworked representation by replacing fDecimalAt
19 * 04/16/97 aliu Rewrote set() and getDouble() to use sprintf/atof
20 * to do digit conversion.
21 * 09/09/97 aliu Modified for exponential notation support.
22 * 08/02/98 stephen Added nearest/even rounding
23 * Fixed bug in fitsIntoLong
24 ******************************************************************************
27 #include "unicode/putil.h"
36 // ***************************************************************************
38 // This class handles the transcoding between numeric values and strings of
39 // characters. Only handles as non-negative numbers.
40 // ***************************************************************************
43 * This is the zero digit. Array elements fDigits[i] have values from
44 * kZero to kZero + 9. Typically, this is '0'.
48 static char gDecimal
= 0;
50 /* Only for 32 bit numbers. Ignore the negative sign. */
51 static const char LONG_MIN_REP
[] = "2147483648";
52 static const char I64_MIN_REP
[] = "9223372036854775808";
54 static const int64_t I64_MIN_VALUE
= U_INT64_MIN
;
57 LONG_MIN_REP_LENGTH
= sizeof(LONG_MIN_REP
) - 1, //Ignore the NULL at the end
58 I64_MIN_REP_LENGTH
= sizeof(I64_MIN_REP
) - 1 //Ignore the NULL at the end
64 // -------------------------------------
65 // default constructor
67 DigitList::DigitList()
69 fDigits
= fDecimalDigits
+ 1; // skip the decimal
73 // -------------------------------------
75 DigitList::~DigitList()
79 // -------------------------------------
82 DigitList::DigitList(const DigitList
&other
)
84 fDigits
= fDecimalDigits
+ 1; // skip the decimal
88 // -------------------------------------
89 // assignment operator
92 DigitList::operator=(const DigitList
& other
)
96 fDecimalAt
= other
.fDecimalAt
;
97 fCount
= other
.fCount
;
98 fIsPositive
= other
.fIsPositive
;
99 fRoundingMode
= other
.fRoundingMode
;
100 uprv_strncpy(fDigits
, other
.fDigits
, fCount
);
105 // -------------------------------------
108 DigitList::operator==(const DigitList
& that
) const
110 return ((this == &that
) ||
111 (fDecimalAt
== that
.fDecimalAt
&&
112 fCount
== that
.fCount
&&
113 fIsPositive
== that
.fIsPositive
&&
114 fRoundingMode
== that
.fRoundingMode
&&
115 uprv_strncmp(fDigits
, that
.fDigits
, fCount
) == 0));
118 // -------------------------------------
119 // Resets the digit list; sets all the digits to zero.
127 fRoundingMode
= DecimalFormat::kRoundHalfEven
;
129 // Don't bother initializing fDigits because fCount is 0.
134 // -------------------------------------
137 * Formats a number into a base 10 string representation, and NULL terminates it.
138 * @param number The number to format
139 * @param outputStr The string to output to
140 * @param outputLen The maximum number of characters to put into outputStr
142 * @return the number of digits written, not including the sign.
145 formatBase10(int64_t number
, char *outputStr
, int32_t outputLen
)
147 char buffer
[MAX_DIGITS
+ 1];
151 if (outputLen
> MAX_DIGITS
) {
152 outputLen
= MAX_DIGITS
; // Ignore NULL
154 else if (outputLen
< 3) {
155 return 0; // Not enough room
158 bufferLen
= outputLen
;
160 if (number
< 0) { // Negative numbers are slightly larger than a postive
161 buffer
[bufferLen
--] = (char)(-(number
% 10) + kZero
);
163 *(outputStr
++) = '-';
166 *(outputStr
++) = '+'; // allow +0
168 while (bufferLen
>= 0 && number
) { // Output the number
169 buffer
[bufferLen
--] = (char)(number
% 10 + kZero
);
173 result
= outputLen
- bufferLen
++;
175 while (bufferLen
<= outputLen
) { // Copy the number to output
176 *(outputStr
++) = buffer
[bufferLen
++];
178 *outputStr
= 0; // NULL terminate.
183 * Currently, getDouble() depends on atof() to do its conversion.
186 * This is an extremely costly function. ~1/2 of the conversion time
187 * can be linked to this function.
190 DigitList::getDouble() /*const*/
200 char rep
[MAX_DIGITS
];
201 // For machines that decide to change the decimal on you,
202 // and try to be too smart with localization.
203 // This normally should be just a '.'.
204 sprintf(rep
, "%+1.1f", 1.0);
208 *fDecimalDigits
= gDecimal
;
209 *(fDigits
+fCount
) = 'e'; // add an e after the digits.
210 formatBase10(fDecimalAt
,
211 fDigits
+ fCount
+ 1, // skip the 'e'
212 MAX_DEC_DIGITS
- fCount
- 3); // skip the 'e' and '.'
213 value
= uprv_strtod(fDecimalDigits
, &end
);
216 return fIsPositive
? value
: -value
;
219 // -------------------------------------
222 * Make sure that fitsIntoLong() is called before calling this function.
224 int32_t DigitList::getLong() /*const*/
226 if (fCount
== fDecimalAt
) {
229 fDigits
[fCount
] = 0; // NULL terminate
231 // This conversion is bad on 64-bit platforms when we want to
232 // be able to return a 64-bit number [grhoten]
233 *fDecimalDigits
= fIsPositive
? '+' : '-';
234 value
= (int32_t)atol(fDecimalDigits
);
238 // This is 100% accurate in c++ because if we are representing
239 // an integral value, we suffer nothing in the conversion to
240 // double. If we are to support 64-bit longs later, getLong()
241 // must be rewritten. [LIU]
242 return (int32_t)getDouble();
248 * Make sure that fitsIntoInt64() is called before calling this function.
250 int64_t DigitList::getInt64() /*const*/
252 if (fCount
== fDecimalAt
) {
255 fDigits
[fCount
] = 0; // NULL terminate
257 // This conversion is bad on 64-bit platforms when we want to
258 // be able to return a 64-bit number [grhoten]
259 *fDecimalDigits
= fIsPositive
? '+' : '-';
261 if (fCount
< LONG_MIN_REP_LENGTH
) {
262 return (int64_t)atol(fDecimalDigits
);
265 // too big for atol, hand-roll atoi64
267 for (int i
= 0; i
< fCount
; ++i
) {
268 int v
= fDigits
[i
] - kZero
;
269 value
= value
* (uint64_t)10 + (uint64_t)v
;
275 int64_t svalue
= (int64_t)value
;
279 // todo: figure out best approach
281 // This is 100% accurate in c++ because if we are representing
282 // an integral value, we suffer nothing in the conversion to
283 // double. If we are to support 64-bit longs later, getLong()
284 // must be rewritten. [LIU]
285 return (int64_t)getDouble();
290 * Return true if the number represented by this object can fit into
294 DigitList::fitsIntoLong(UBool ignoreNegativeZero
) /*const*/
296 // Figure out if the result will fit in a long. We have to
297 // first look for nonzero digits after the decimal point;
298 // then check the size.
300 // Trim trailing zeros after the decimal point. This does not change
301 // the represented value.
302 while (fCount
> fDecimalAt
&& fCount
> 0 && fDigits
[fCount
- 1] == kZero
)
306 // Positive zero fits into a long, but negative zero can only
307 // be represented as a double. - bug 4162852
308 return fIsPositive
|| ignoreNegativeZero
;
311 // If the digit list represents a double or this number is too
313 if (fDecimalAt
< fCount
|| fDecimalAt
> LONG_MIN_REP_LENGTH
)
316 // If number is small enough to fit in a long
317 if (fDecimalAt
< LONG_MIN_REP_LENGTH
)
320 // At this point we have fDecimalAt == fCount, and fCount == LONG_MIN_REP_LENGTH.
321 // The number will overflow if it is larger than LONG_MAX
322 // or smaller than LONG_MIN.
323 for (int32_t i
=0; i
<fCount
; ++i
)
325 char dig
= fDigits
[i
],
326 max
= LONG_MIN_REP
[i
];
333 // At this point the first count digits match. If fDecimalAt is less
334 // than count, then the remaining digits are zero, and we return true.
335 if (fCount
< fDecimalAt
)
338 // Now we have a representation of Long.MIN_VALUE, without the leading
339 // negative sign. If this represents a positive value, then it does
340 // not fit; otherwise it fits.
345 * Return true if the number represented by this object can fit into
349 DigitList::fitsIntoInt64(UBool ignoreNegativeZero
) /*const*/
351 // Figure out if the result will fit in a long. We have to
352 // first look for nonzero digits after the decimal point;
353 // then check the size.
355 // Trim trailing zeros after the decimal point. This does not change
356 // the represented value.
357 while (fCount
> fDecimalAt
&& fCount
> 0 && fDigits
[fCount
- 1] == kZero
)
361 // Positive zero fits into a long, but negative zero can only
362 // be represented as a double. - bug 4162852
363 return fIsPositive
|| ignoreNegativeZero
;
366 // If the digit list represents a double or this number is too
368 if (fDecimalAt
< fCount
|| fDecimalAt
> I64_MIN_REP_LENGTH
)
371 // If number is small enough to fit in an int64
372 if (fDecimalAt
< I64_MIN_REP_LENGTH
)
375 // At this point we have fDecimalAt == fCount, and fCount == INT64_MIN_REP_LENGTH.
376 // The number will overflow if it is larger than U_INT64_MAX
377 // or smaller than U_INT64_MIN.
378 for (int32_t i
=0; i
<fCount
; ++i
)
380 char dig
= fDigits
[i
],
381 max
= I64_MIN_REP
[i
];
388 // At this point the first count digits match. If fDecimalAt is less
389 // than count, then the remaining digits are zero, and we return true.
390 if (fCount
< fDecimalAt
)
393 // Now we have a representation of INT64_MIN_VALUE, without the leading
394 // negative sign. If this represents a positive value, then it does
395 // not fit; otherwise it fits.
400 // -------------------------------------
403 DigitList::set(int32_t source
, int32_t maximumDigits
)
405 set((int64_t)source
, maximumDigits
);
408 // -------------------------------------
410 * @param maximumDigits The maximum digits to be generated. If zero,
411 * there is no maximum -- generate all digits.
414 DigitList::set(int64_t source
, int32_t maximumDigits
)
416 fCount
= fDecimalAt
= formatBase10(source
, fDecimalDigits
, MAX_DIGITS
);
418 fIsPositive
= (*fDecimalDigits
== '+');
420 // Don't copy trailing zeros
421 while (fCount
> 1 && fDigits
[fCount
- 1] == kZero
)
424 if(maximumDigits
> 0)
425 round(maximumDigits
);
429 * Set the digit list to a representation of the given double value.
430 * This method supports both fixed-point and exponential notation.
431 * @param source Value to be converted; must not be Inf, -Inf, Nan,
433 * @param maximumDigits The most fractional or total digits which should
434 * be converted. If total digits, and the value is zero, then
435 * there is no maximum -- generate all digits.
436 * @param fixedPoint If true, then maximumDigits is the maximum
437 * fractional digits to be converted. If false, total digits.
440 DigitList::set(double source
, int32_t maximumDigits
, UBool fixedPoint
)
442 // for now, simple implementation; later, do proper IEEE stuff
443 char rep
[MAX_DIGITS
+ 8]; // Extra space for '+', '.', e+NNN, and '\0' (actually +8 is enough)
444 char *digitPtr
= fDigits
;
445 char *repPtr
= rep
+ 2; // +2 to skip the sign and decimal
446 int32_t exponent
= 0;
448 fIsPositive
= !uprv_isNegative(source
); // Allow +0 and -0
450 // Generate a representation of the form /[+-][0-9]+e[+-][0-9]+/
451 sprintf(rep
, "%+1.*e", MAX_DBL_DIGITS
- 1, source
);
453 rep
[2] = rep
[1]; // remove decimal
455 while (*repPtr
== kZero
) {
457 fDecimalAt
--; // account for leading zeros
460 while (*repPtr
!= 'e') {
461 *(digitPtr
++) = *(repPtr
++);
463 fCount
= MAX_DBL_DIGITS
+ fDecimalAt
;
465 // Parse an exponent of the form /[eE][+-][0-9]+/
466 UBool negExp
= (*(++repPtr
) == '-');
467 while (*(++repPtr
) != 0) {
468 exponent
= 10*exponent
+ *repPtr
- kZero
;
471 exponent
= -exponent
;
473 fDecimalAt
+= exponent
+ 1; // +1 for decimal removal
475 // The negative of the exponent represents the number of leading
476 // zeros between the decimal and the first non-zero digit, for
477 // a value < 0.1 (e.g., for 0.00123, -decimalAt == 2). If this
478 // is more than the maximum fraction digits, then we have an underflow
479 // for the printed representation.
480 if (fixedPoint
&& -fDecimalAt
>= maximumDigits
)
482 // If we round 0.0009 to 3 fractional digits, then we have to
483 // create a new one digit in the least significant location.
484 if (-fDecimalAt
== maximumDigits
&& shouldRoundUp(0)) {
487 fDigits
[0] = (char)'1';
489 // Handle an underflow to zero when we round something like
490 // 0.0009 to 2 fractional digits.
497 // Eliminate digits beyond maximum digits to be displayed.
498 // Round up if appropriate. Do NOT round in the special
499 // case where maximumDigits == 0 and fixedPoint is FALSE.
500 if (fixedPoint
|| (0 < maximumDigits
&& maximumDigits
< fCount
)) {
501 round(fixedPoint
? (maximumDigits
+ fDecimalAt
) : maximumDigits
);
504 // Eliminate trailing zeros.
505 while (fCount
> 1 && fDigits
[fCount
- 1] == kZero
)
510 // -------------------------------------
513 * Round the representation to the given number of digits.
514 * @param maximumDigits The maximum number of digits to be shown.
515 * Upon return, count will be less than or equal to maximumDigits.
518 DigitList::round(int32_t maximumDigits
)
520 // Eliminate digits beyond maximum digits to be displayed.
521 // Round up if appropriate.
522 if (maximumDigits
>= 0 && maximumDigits
< fCount
)
524 if (shouldRoundUp(maximumDigits
)) {
525 // Rounding up involved incrementing digits from LSD to MSD.
526 // In most cases this is simple, but in a worst case situation
527 // (9999..99) we have to adjust the decimalAt value.
528 while (--maximumDigits
>= 0 && ++fDigits
[maximumDigits
] > '9')
531 if (maximumDigits
< 0)
533 // We have all 9's, so we increment to a single digit
534 // of one and adjust the exponent.
535 fDigits
[0] = (char) '1';
537 maximumDigits
= 1; // Adjust the count
541 ++maximumDigits
; // Increment for use as count
544 fCount
= maximumDigits
;
547 // Eliminate trailing zeros.
548 while (fCount
> 1 && fDigits
[fCount
-1] == kZero
) {
554 * Return true if truncating the representation to the given number
555 * of digits will result in an increment to the last digit. This
556 * method implements the requested rounding mode.
558 * @param maximumDigits the number of digits to keep, from 0 to
559 * <code>count-1</code>. If 0, then all digits are rounded away, and
560 * this method returns true if a one should be generated (e.g., formatting
562 * @return true if digit <code>maximumDigits-1</code> should be
565 UBool
DigitList::shouldRoundUp(int32_t maximumDigits
) const {
566 switch (fRoundingMode
) {
567 case DecimalFormat::kRoundCeiling
:
569 case DecimalFormat::kRoundFloor
:
571 case DecimalFormat::kRoundDown
:
573 case DecimalFormat::kRoundUp
:
575 case DecimalFormat::kRoundHalfEven
:
576 case DecimalFormat::kRoundHalfDown
:
577 case DecimalFormat::kRoundHalfUp
:
579 if (fDigits
[maximumDigits
] == '5' ) {
580 for (int i
=maximumDigits
+1; i
<fCount
; ++i
) {
581 if (fDigits
[i
] != kZero
) {
585 switch (fRoundingMode
) {
586 case DecimalFormat::kRoundHalfEven
:
588 // Implement IEEE half-even rounding
589 return maximumDigits
> 0 && (fDigits
[maximumDigits
-1] % 2 != 0);
590 case DecimalFormat::kRoundHalfDown
:
592 case DecimalFormat::kRoundHalfUp
:
596 return (fDigits
[maximumDigits
] > '5');
600 // -------------------------------------
602 // In the Java implementation, we need a separate set(long) because 64-bit longs
603 // have too much precision to fit into a 64-bit double. In C++, longs can just
604 // be passed to set(double) as long as they are 32 bits in size. We currently
605 // don't implement 64-bit longs in C++, although the code below would work for
606 // that with slight modifications. [LIU]
609 DigitList::set(long source)
611 // handle the special case of zero using a standard exponent of 0.
612 // mathematically, the exponent can be any value.
620 // we don't accept negative numbers, with the exception of long_min.
621 // long_min is treated specially by being represented as long_max+1,
622 // which is actually an impossible signed long value, so there is no
623 // ambiguity. we do this for convenience, so digitlist can easily
624 // represent the digits of a long.
625 bool islongmin = (source == long_min);
628 source = -(source + 1); // that is, long_max
631 sprintf(fdigits, "%d", source);
633 // now we need to compute the exponent. it's easy in this case; it's
634 // just the same as the count. e.g., 0.123 * 10^3 = 123.
635 fcount = strlen(fdigits);
638 // here's how we represent long_max + 1. note that we always know
639 // that the last digit of long_max will not be 9, because long_max
640 // is of the form (2^n)-1.
644 // finally, we trim off trailing zeros. we don't alter fDecimalAt,
645 // so this has no effect on the represented value. we know the first
646 // digit is non-zero (see code above), so we only have to check down
648 while (fcount > 1 && fdigits[fcount-1] == kzero)
654 * Return true if this object represents the value zero. Anything with
655 * no digits, or all zero digits, is zero, regardless of fDecimalAt.
658 DigitList::isZero() const
660 for (int32_t i
=0; i
<fCount
; ++i
)
661 if (fDigits
[i
] != kZero
)