decimal.h

Go to the documentation of this file.
00001 // OSB library ************************************************ -*- C++ -*-
00006 /*
00007   AUTHOR(S): Vipul Gupta (vg)
00008 
00009   RCS information
00010    $Name: OSB_060808 $
00011    $Revision: 1.38 $
00012 
00013   License
00014    OSB rating and billing library for communication networks
00015    Copyright (C) 2004, 2005, 2006  OSB systems
00016 
00017    This file may be distributed and/or modify under the terms of the
00018    GNU General Public License (GPL) as published by the Free Software
00019    Foundation which is provided in the file LICENSE.GPL included in the
00020    packaging of this file.
00021 
00022    The file is distributed in the hope that it will be useful, but WITHOUT
00023    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
00024    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
00025    for more details.
00026 
00027    Holders of a OSB Commercial License may use this file under the terms
00028    and conditions of this commercial license.
00029  */
00030 #ifndef _DECIMAL_H_
00031 #define _DECIMAL_H_
00032 
00033 // *********************************************************************
00034 // included header files
00035 // + standard includes
00036 #include <string>
00037 #include <iosfwd>
00038 #include <utility>
00039 #include <cmath>
00040 #include <limits>
00041 
00042 // + local headers
00043 #include "osberror.h"
00044 
00045 // *********************************************************************
00046 // namespace extensions
00047 namespace OSB_LIB {
00048 
00050     class Decimal
00051     {
00052         friend class RoundUp;
00053         friend bool operator==(const Decimal&, const Decimal&);
00054         friend bool operator!=(const Decimal&, const Decimal&);
00055         friend bool operator<(const Decimal&, const Decimal&);
00056         friend bool operator>(const Decimal&, const Decimal&);
00057         friend bool operator<=(const Decimal&, const Decimal&);
00058         friend bool operator>=(const Decimal&, const Decimal&);
00059 
00060     public:
00062         static const long long valueMax = 0x7fffffffffffffffLL;
00064         static const long long valueMin = ~valueMax;
00065 
00066     public:
00067         Decimal() { value_ = 0; }
00068         Decimal(int v) { value_ = longToDecimal(v); }
00069         Decimal(long v) { value_ = longToDecimal(v); }
00070         Decimal(long long v) { value_ = longToDecimal(v); }
00071         Decimal(double v) { value_ = doubleToDecimal(v); }
00072         Decimal(const Decimal& d) : value_(d.value_) {}
00073         Decimal& operator=(const Decimal&);
00074         Decimal& operator-();
00075 
00077         double toDouble() const;
00078 
00080         double frac() const;
00081 
00083         long trunc() const;
00085         long ceil() const;
00086 
00088         std::string str() const;
00089 
00090         Decimal& operator+=(const Decimal&);
00091         Decimal& operator-=(const Decimal&);
00092         Decimal& operator*=(const Decimal&);
00093         Decimal& operator/=(const Decimal&);
00094         static long power() { return power_; }
00095         static int precision(int p =-1);     //Retreive the old precision
00096                                              // and set the new precision
00097 
00098     private:
00099         long long doubleToDecimal(double);   //Convert double to
00100                                              //"Decimal"
00101 
00102         long long longToDecimal(long long);  //Convert long or
00103                                              //int to "Decimal"
00104 
00105         static int precision_;               //Number of digits after the
00106                                              //decimal,of the "value_"
00107                                              //to be displayed after the
00108                                              //rounding has been done.
00109 
00110         static long power_;                  //Value required to convert
00111                                              //a double, long or integer
00112                                              //value to a "Decimal"
00113 
00114         long long value_;                    //The value of a
00115                                              //Decimal Object.
00116     };
00117 
00118     inline Decimal operator++(Decimal&);            //prefix operator
00119     inline Decimal operator--(Decimal&);
00120     inline Decimal operator++(Decimal&, int);       //postfix operator
00121     inline Decimal operator--(Decimal&, int);
00122     inline Decimal operator*(const Decimal&, const Decimal&);
00123     inline Decimal operator/(const Decimal&, const Decimal&);
00124     inline Decimal operator+(const Decimal&, const Decimal&);
00125     inline Decimal operator-(const Decimal&, const Decimal&);
00126 
00127     std::ostream& operator<<(std::ostream&, const Decimal&);
00128 
00129     inline Decimal& Decimal::operator=(const Decimal& d)
00130     {
00131         if (this != &d) value_ = d.value_;
00132         return *this;
00133     }
00134 
00135     inline Decimal& Decimal::operator-()
00136     {
00137         value_ *= -1;
00138         return *this;
00139     }
00140 
00141     inline Decimal operator+(const Decimal& d1, const Decimal& d2)
00142     {
00143         Decimal r(d1);
00144         return r += d2;
00145     }
00146 
00147     inline Decimal operator-(const Decimal& d1, const Decimal& d2)
00148     {
00149         Decimal r(d1);
00150         return r -= d2;
00151     }
00152 
00153     inline Decimal operator*(const Decimal& d1, const Decimal& d2)
00154     {
00155         Decimal r(d1);
00156         return r *= d2;
00157     }
00158 
00159     inline Decimal operator/(const Decimal& d1, const Decimal& d2)
00160     {
00161         Decimal r(d1);
00162         return r /= d2;
00163     }
00164 
00165     inline Decimal operator++(Decimal& d)
00166     {
00167         return d += 1;
00168     }
00169 
00170     inline Decimal operator++(Decimal& d, int)
00171     {
00172         Decimal r = d;
00173         d += 1;
00174         return r;
00175     }
00176 
00177     inline Decimal operator--(Decimal& d, int)
00178     {
00179         Decimal r = d;
00180         d -= 1;
00181         return r;
00182     }
00183 
00184     inline Decimal operator--(Decimal& d)
00185     {
00186         return d -= 1;
00187     }
00188 
00189     inline bool operator==(const Decimal& d1, const Decimal& d2)
00190     {
00191         return d1.value_ == d2.value_;
00192     }
00193 
00194     inline bool operator!=(const Decimal& d1, const Decimal& d2)
00195     {
00196         return d1.value_ != d2.value_;
00197     }
00198 
00199     inline bool operator<(const Decimal& d1, const Decimal& d2)
00200     {
00201         return d1.value_ < d2.value_;
00202     }
00203 
00204     inline bool operator>(const Decimal& d1, const Decimal& d2)
00205     {
00206         return d1.value_ > d2.value_;
00207     }
00208 
00209     inline bool operator<=(const Decimal& d1, const Decimal& d2)
00210     {
00211         return d1.value_ <= d2.value_;
00212     }
00213 
00214     inline bool operator>=(const Decimal& d1, const Decimal& d2)
00215     {
00216         return d1.value_ >= d2.value_;
00217     }
00218 }                                   // namespace OSB_LIB
00219 #endif                              // #ifndef _DECIMAL_H_

Generated on Sat Sep 2 14:06:32 2006 for OSB Library by  doxygen 1.4.7