1999-08-16 21:50:52 +00:00
|
|
|
/*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-12-13 22:28:37 +00:00
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
* Copyright (C) 1997-2001, International Business Machines
|
1999-12-13 22:28:37 +00:00
|
|
|
* Corporation and others. All Rights Reserved.
|
|
|
|
*
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*
|
|
|
|
* File DIGITLST.H
|
|
|
|
*
|
|
|
|
* Modification History:
|
|
|
|
*
|
|
|
|
* Date Name Description
|
|
|
|
* 02/25/97 aliu Converted from java.
|
|
|
|
* 03/21/97 clhuang Updated per C++ implementation.
|
|
|
|
* 04/15/97 aliu Changed MAX_COUNT to DBL_DIG. Changed Digit to char.
|
|
|
|
* 09/09/97 aliu Adapted for exponential notation support.
|
|
|
|
* 08/02/98 stephen Added nearest/even rounding
|
|
|
|
* 06/29/99 stephen Made LONG_DIGITS a macro to satisfy SUN compiler
|
|
|
|
* 07/09/99 stephen Removed kMaxCount (unused, for HP compiler)
|
2001-03-21 20:44:20 +00:00
|
|
|
******************************************************************************
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef DIGITLST_H
|
|
|
|
#define DIGITLST_H
|
|
|
|
|
1999-12-28 23:39:02 +00:00
|
|
|
#include "unicode/utypes.h"
|
2002-06-27 01:19:20 +00:00
|
|
|
#include "unicode/uobject.h"
|
1999-08-16 21:50:52 +00:00
|
|
|
#include <float.h>
|
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
// Decimal digits in a 64-bit int
|
2000-10-12 19:04:06 +00:00
|
|
|
//#define LONG_DIGITS 19
|
2003-10-31 22:47:25 +00:00
|
|
|
#define INT64_DIGITS 19
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-07-09 23:42:00 +00:00
|
|
|
typedef enum EDigitListValues {
|
2003-10-31 22:47:25 +00:00
|
|
|
MAX_DBL_DIGITS = DBL_DIG,
|
|
|
|
MAX_I64_DIGITS = INT64_DIGITS,
|
|
|
|
MAX_DIGITS = MAX_I64_DIGITS,
|
2001-07-09 23:42:00 +00:00
|
|
|
MAX_EXPONENT = DBL_DIG,
|
|
|
|
DIGIT_PADDING = 3,
|
|
|
|
|
|
|
|
// "+." + fDigits + "e" + fDecimalAt
|
2003-10-31 22:47:25 +00:00
|
|
|
MAX_DEC_DIGITS = MAX_DIGITS + DIGIT_PADDING + MAX_EXPONENT
|
2001-07-09 23:42:00 +00:00
|
|
|
} EDigitListValues;
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_BEGIN
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
2002-11-30 02:43:51 +00:00
|
|
|
* Digit List utility class. Private to DecimalFormat. Handles the transcoding
|
1999-08-16 21:50:52 +00:00
|
|
|
* between numeric values and strings of characters. Only handles
|
|
|
|
* non-negative numbers. The division of labor between DigitList and
|
|
|
|
* DecimalFormat is that DigitList handles the radix 10 representation
|
|
|
|
* issues; DecimalFormat handles the locale-specific issues such as
|
|
|
|
* positive/negative, grouping, decimal point, currency, and so on.
|
|
|
|
* <P>
|
|
|
|
* A DigitList is really a representation of a floating point value.
|
|
|
|
* It may be an integer value; we assume that a double has sufficient
|
|
|
|
* precision to represent all digits of a long.
|
|
|
|
* <P>
|
|
|
|
* The DigitList representation consists of a string of characters,
|
|
|
|
* which are the digits radix 10, from '0' to '9'. It also has a radix
|
|
|
|
* 10 exponent associated with it. The value represented by a DigitList
|
|
|
|
* object can be computed by mulitplying the fraction f, where 0 <= f < 1,
|
|
|
|
* derived by placing all the digits of the list to the right of the
|
|
|
|
* decimal point, by 10^exponent.
|
|
|
|
*/
|
2003-10-31 22:47:25 +00:00
|
|
|
class U_I18N_API DigitList : public UMemory { // Declare external to make compiler happy
|
1999-08-16 21:50:52 +00:00
|
|
|
public:
|
|
|
|
DigitList();
|
2000-04-18 17:58:18 +00:00
|
|
|
~DigitList();
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2002-07-03 12:05:56 +00:00
|
|
|
/* copy constructor
|
|
|
|
* @param DigitList The object to be copied.
|
|
|
|
* @return the newly created object.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
DigitList(const DigitList&); // copy constructor
|
|
|
|
|
2002-07-03 12:05:56 +00:00
|
|
|
/* assignment operator
|
|
|
|
* @param DigitList The object to be copied.
|
|
|
|
* @return the newly created object.
|
|
|
|
*/
|
1999-08-16 21:50:52 +00:00
|
|
|
DigitList& operator=(const DigitList&); // assignment operator
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if another object is semantically equal to this one.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param other The DigitList to be compared for equality
|
|
|
|
* @return true if another object is semantically equal to this one.
|
|
|
|
* return false otherwise.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool operator==(const DigitList& other) const;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if another object is semantically unequal to this one.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param other The DigitList to be compared for inequality
|
|
|
|
* @return true if another object is semantically unequal to this one.
|
|
|
|
* return false otherwise.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool operator!=(const DigitList& other) const { return !operator==(other); }
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Clears out the digits.
|
|
|
|
* Use before appending them.
|
|
|
|
* Typically, you set a series of digits with append, then at the point
|
|
|
|
* you hit the decimal point, you set myDigitList.fDecimalAt = myDigitList.fCount;
|
|
|
|
* then go on appending digits.
|
|
|
|
*/
|
2000-04-18 17:58:18 +00:00
|
|
|
void clear(void);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Appends digits to the list. Ignores all digits beyond the first DBL_DIG,
|
|
|
|
* since they are not significant for either longs or doubles.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param digit The digit to be appended.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-10-12 19:04:06 +00:00
|
|
|
inline void append(char digit);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility routine to get the value of the digit list
|
|
|
|
* Returns 0.0 if zero length.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @return the value of the digit list.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-11-07 18:57:11 +00:00
|
|
|
double getDouble(void);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility routine to get the value of the digit list
|
2000-10-24 16:09:39 +00:00
|
|
|
* Make sure that fitsIntoLong() is called before calling this function.
|
1999-08-16 21:50:52 +00:00
|
|
|
* Returns 0 if zero length.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @return the value of the digit list, return 0 if it is zero length
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-10-12 19:04:06 +00:00
|
|
|
int32_t getLong(void);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
/**
|
|
|
|
* Utility routine to get the value of the digit list
|
|
|
|
* Make sure that fitsIntoInt64() is called before calling this function.
|
|
|
|
* Returns 0 if zero length.
|
|
|
|
* @return the value of the digit list, return 0 if it is zero length
|
|
|
|
*/
|
|
|
|
int64_t getInt64(void);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Return true if the number represented by this object can fit into
|
|
|
|
* a long.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param ignoreNegativeZero True if negative zero is ignored.
|
|
|
|
* @return true if the number represented by this object can fit into
|
|
|
|
* a long, return false otherwise.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-11-02 02:42:40 +00:00
|
|
|
UBool fitsIntoLong(UBool ignoreNegativeZero);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
/**
|
|
|
|
* Return true if the number represented by this object can fit into
|
|
|
|
* an int64_t.
|
|
|
|
* @param ignoreNegativeZero True if negative zero is ignored.
|
|
|
|
* @return true if the number represented by this object can fit into
|
|
|
|
* a long, return false otherwise.
|
|
|
|
*/
|
|
|
|
UBool fitsIntoInt64(UBool ignoreNegativeZero);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Utility routine to set the value of the digit list from a double
|
|
|
|
* Input must be non-negative, and must not be Inf, -Inf, or NaN.
|
|
|
|
* The maximum fraction digits helps us round properly.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param source The value to be set
|
|
|
|
* @param maximunDigits The maximum number of digits to be shown
|
|
|
|
* @param fixedPoint True if the point is fixed
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
void set(double source, int32_t maximumDigits, UBool fixedPoint = TRUE);
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Utility routine to set the value of the digit list from a long.
|
|
|
|
* If a non-zero maximumDigits is specified, no more than that number of
|
|
|
|
* significant digits will be produced.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param source The value to be set
|
|
|
|
* @param maximunDigits The maximum number of digits to be shown
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
|
|
|
void set(int32_t source, int32_t maximumDigits = 0);
|
|
|
|
|
2003-10-31 22:47:25 +00:00
|
|
|
/**
|
|
|
|
* Utility routine to set the value of the digit list from an int64.
|
|
|
|
* If a non-zero maximumDigits is specified, no more than that number of
|
|
|
|
* significant digits will be produced.
|
|
|
|
* @param source The value to be set
|
|
|
|
* @param maximunDigits The maximum number of digits to be shown
|
|
|
|
*/
|
|
|
|
void set(int64_t source, int32_t maximumDigits = 0);
|
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Return true if this is a representation of zero.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @return true if this is a representation of zero.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool isZero(void) const;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if this is a representation of LONG_MIN. You must use
|
|
|
|
* this method to determine if this is so; you cannot check directly,
|
|
|
|
* because a special format is used to handle this.
|
|
|
|
*/
|
2002-03-26 23:09:05 +00:00
|
|
|
// This code is unused.
|
|
|
|
//UBool isLONG_MIN(void) const;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* These data members are intentionally public and can be set directly.
|
|
|
|
*<P>
|
|
|
|
* The value represented is given by placing the decimal point before
|
|
|
|
* fDigits[fDecimalAt]. If fDecimalAt is < 0, then leading zeros between
|
|
|
|
* the decimal point and the first nonzero digit are implied. If fDecimalAt
|
|
|
|
* is > fCount, then trailing zeros between the fDigits[fCount-1] and the
|
|
|
|
* decimal point are implied.
|
|
|
|
* <P>
|
|
|
|
* Equivalently, the represented value is given by f * 10^fDecimalAt. Here
|
|
|
|
* f is a value 0.1 <= f < 1 arrived at by placing the digits in fDigits to
|
|
|
|
* the right of the decimal.
|
|
|
|
* <P>
|
|
|
|
* DigitList is normalized, so if it is non-zero, fDigits[0] is non-zero. We
|
|
|
|
* don't allow denormalized numbers because our exponent is effectively of
|
|
|
|
* unlimited magnitude. The fCount value contains the number of significant
|
|
|
|
* digits present in fDigits[].
|
|
|
|
* <P>
|
|
|
|
* Zero is represented by any DigitList with fCount == 0 or with each fDigits[i]
|
|
|
|
* for all i <= fCount == '0'.
|
|
|
|
*/
|
|
|
|
int32_t fDecimalAt;
|
|
|
|
int32_t fCount;
|
2000-11-02 02:42:40 +00:00
|
|
|
UBool fIsPositive;
|
2000-10-24 16:09:39 +00:00
|
|
|
char *fDigits;
|
1999-08-16 21:50:52 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
|
2000-11-07 18:57:11 +00:00
|
|
|
/* One character before fDigits for the decimal*/
|
|
|
|
char fDecimalDigits[MAX_DEC_DIGITS + 1];
|
2000-10-24 16:09:39 +00:00
|
|
|
|
1999-08-16 21:50:52 +00:00
|
|
|
/**
|
|
|
|
* Round the representation to the given number of digits.
|
|
|
|
* @param maximumDigits The maximum number of digits to be shown.
|
|
|
|
* Upon return, count will be less than or equal to maximumDigits.
|
|
|
|
*/
|
|
|
|
void round(int32_t maximumDigits);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initializes the buffer that records the mimimum long value.
|
2002-07-03 12:05:56 +00:00
|
|
|
* @param maximumDigits The maximum number of digits to be shown.
|
1999-08-16 21:50:52 +00:00
|
|
|
*/
|
2000-10-12 19:04:06 +00:00
|
|
|
/*static void initializeLONG_MIN_REP(void);*/
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2000-05-18 22:08:39 +00:00
|
|
|
UBool shouldRoundUp(int32_t maximumDigits);
|
1999-08-16 21:50:52 +00:00
|
|
|
};
|
|
|
|
|
2000-10-12 19:04:06 +00:00
|
|
|
// -------------------------------------
|
|
|
|
// Appends the digit to the digit list if it's not out of scope.
|
|
|
|
// Ignores the digit, otherwise.
|
|
|
|
|
|
|
|
inline void
|
|
|
|
DigitList::append(char digit)
|
|
|
|
{
|
|
|
|
// Ignore digits which exceed the precision we can represent
|
|
|
|
if (fCount < MAX_DIGITS)
|
|
|
|
fDigits[fCount++] = digit;
|
|
|
|
}
|
|
|
|
|
2001-10-08 23:26:58 +00:00
|
|
|
U_NAMESPACE_END
|
2002-11-28 01:27:35 +00:00
|
|
|
#endif // _DIGITLST
|
1999-08-16 21:50:52 +00:00
|
|
|
|
2001-07-09 23:42:00 +00:00
|
|
|
//eof
|