home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / common / digitlst.h < prev    next >
Encoding:
C/C++ Source or Header  |  1999-08-16  |  6.9 KB  |  195 lines

  1. /*
  2. ********************************************************************************
  3. *                                                                              *
  4. * COPYRIGHT:                                                                   *
  5. *   (C) Copyright Taligent, Inc.,  1997                                        *
  6. *   (C) Copyright International Business Machines Corporation,  1997-1999      *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.         *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure    *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                     *
  10. *                                                                              *
  11. ********************************************************************************
  12. *
  13. * File DIGITLST.H
  14. *
  15. * Modification History:
  16. *
  17. *   Date        Name        Description
  18. *   02/25/97    aliu        Converted from java.
  19. *   03/21/97    clhuang     Updated per C++ implementation.
  20. *   04/15/97    aliu        Changed MAX_COUNT to DBL_DIG.  Changed Digit to char.
  21. *   09/09/97    aliu        Adapted for exponential notation support.
  22. *   08/02/98    stephen     Added nearest/even rounding
  23. *   06/29/99    stephen     Made LONG_DIGITS a macro to satisfy SUN compiler
  24. *   07/09/99    stephen     Removed kMaxCount (unused, for HP compiler)
  25. *******************************************************************************
  26. */
  27.  
  28. #ifndef DIGITLST_H
  29. #define DIGITLST_H
  30.  
  31. #include "utypes.h"
  32. #include <float.h>
  33.  
  34. // Decimal digits in a 32-bit int
  35. #define LONG_DIGITS 19 
  36.  
  37. /**
  38.  * Digit List. Private to DecimalFormat.  Handles the transcoding
  39.  * between numeric values and strings of characters.  Only handles
  40.  * non-negative numbers.  The division of labor between DigitList and
  41.  * DecimalFormat is that DigitList handles the radix 10 representation
  42.  * issues; DecimalFormat handles the locale-specific issues such as
  43.  * positive/negative, grouping, decimal point, currency, and so on.
  44.  * <P>
  45.  * A DigitList is really a representation of a floating point value.
  46.  * It may be an integer value; we assume that a double has sufficient
  47.  * precision to represent all digits of a long.
  48.  * <P>
  49.  * The DigitList representation consists of a string of characters,
  50.  * which are the digits radix 10, from '0' to '9'.  It also has a radix
  51.  * 10 exponent associated with it.  The value represented by a DigitList
  52.  * object can be computed by mulitplying the fraction f, where 0 <= f < 1,
  53.  * derived by placing all the digits of the list to the right of the
  54.  * decimal point, by 10^exponent.
  55.  */
  56. class U_COMMON_API DigitList { // Declare external to make compiler happy
  57. public:
  58.     DigitList();
  59.  
  60.     ~DigitList(); // Make this virtual if subclassing is desired later
  61.  
  62.     DigitList(const DigitList&); // copy constructor
  63.  
  64.     DigitList& operator=(const DigitList&);  // assignment operator
  65.  
  66.     /**
  67.      * Return true if another object is semantically equal to this one.
  68.      */
  69.     bool_t operator==(const DigitList& other) const;
  70.  
  71.     /**
  72.      * Return true if another object is semantically unequal to this one.
  73.      */
  74.     bool_t operator!=(const DigitList& other) const { return !operator==(other); }
  75.  
  76.     /**
  77.      * Clears out the digits.
  78.      * Use before appending them.
  79.      * Typically, you set a series of digits with append, then at the point
  80.      * you hit the decimal point, you set myDigitList.fDecimalAt = myDigitList.fCount;
  81.      * then go on appending digits.
  82.      */
  83.     virtual void clear(void);
  84.  
  85.     /**
  86.      * Appends digits to the list. Ignores all digits beyond the first DBL_DIG,
  87.      * since they are not significant for either longs or doubles.
  88.      */
  89.     virtual void append(char digit);
  90.  
  91.     /**
  92.      * Utility routine to get the value of the digit list
  93.      * Returns 0.0 if zero length.
  94.      */
  95.     double getDouble(void) const;
  96.  
  97.     /**
  98.      * Utility routine to get the value of the digit list
  99.      * Returns 0 if zero length.
  100.      */
  101.     int32_t getLong(void) const;
  102.  
  103.     /**
  104.      * Return true if the number represented by this object can fit into
  105.      * a long.
  106.      */
  107.     bool_t fitsIntoLong(bool_t isPositive, bool_t ignoreNegativeZero);
  108.  
  109.     /**
  110.      * Utility routine to set the value of the digit list from a double
  111.      * Input must be non-negative, and must not be Inf, -Inf, or NaN.
  112.      * The maximum fraction digits helps us round properly.
  113.      */
  114.     void set(double source, int32_t maximumDigits, bool_t fixedPoint = TRUE);
  115.  
  116.     /**
  117.      * Utility routine to set the value of the digit list from a long.
  118.      * If a non-zero maximumDigits is specified, no more than that number of
  119.      * significant digits will be produced.
  120.      */
  121.     void set(int32_t source, int32_t maximumDigits = 0);
  122.  
  123.     /**
  124.      * Return true if this is a representation of zero.
  125.      */
  126.     bool_t isZero(void) const;
  127.  
  128.     /**
  129.      * Return true if this is a representation of LONG_MIN.  You must use
  130.      * this method to determine if this is so; you cannot check directly,
  131.      * because a special format is used to handle this.
  132.      */
  133.     bool_t isLONG_MIN(void) const;
  134.  
  135.     /**
  136.      * This is the zero digit.  Array elements fDigits[i] have values from
  137.      * kZero to kZero + 9.  Typically, this is '0'.
  138.      */
  139.     static const char kZero;
  140.  
  141. public:
  142.     /**
  143.      * These data members are intentionally public and can be set directly.
  144.      *<P>
  145.      * The value represented is given by placing the decimal point before
  146.      * fDigits[fDecimalAt].  If fDecimalAt is < 0, then leading zeros between
  147.      * the decimal point and the first nonzero digit are implied.  If fDecimalAt
  148.      * is > fCount, then trailing zeros between the fDigits[fCount-1] and the
  149.      * decimal point are implied.
  150.      * <P>
  151.      * Equivalently, the represented value is given by f * 10^fDecimalAt.  Here
  152.      * f is a value 0.1 <= f < 1 arrived at by placing the digits in fDigits to
  153.      * the right of the decimal.
  154.      * <P>
  155.      * DigitList is normalized, so if it is non-zero, fDigits[0] is non-zero.  We
  156.      * don't allow denormalized numbers because our exponent is effectively of
  157.      * unlimited magnitude.  The fCount value contains the number of significant
  158.      * digits present in fDigits[].
  159.      * <P>
  160.      * Zero is represented by any DigitList with fCount == 0 or with each fDigits[i]
  161.      * for all i <= fCount == '0'.
  162.      */
  163.     int32_t     fDecimalAt;
  164.     int32_t     fCount;
  165. private:
  166.     enum      { MAX_DIGITS = DBL_DIG };
  167. public:
  168.     char        fDigits[MAX_DIGITS];
  169.  
  170. private:
  171.  
  172.     /**
  173.      * Round the representation to the given number of digits.
  174.      * @param maximumDigits The maximum number of digits to be shown.
  175.      * Upon return, count will be less than or equal to maximumDigits.
  176.      */
  177.     void round(int32_t maximumDigits);
  178.  
  179.     /**
  180.      * Initializes the buffer that records the mimimum long value.
  181.      */
  182.     static void initializeLONG_MIN_REP(void);
  183.  
  184.     bool_t shouldRoundUp(int32_t maximumDigits);
  185.  
  186.     static char LONG_MIN_REP[LONG_DIGITS];
  187.     static int32_t  LONG_MIN_REP_LENGTH;
  188. };
  189.  
  190. #endif // _DIGITLST
  191. //eof
  192.  
  193.  
  194.  
  195.