• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List
  • File Members

spl/Decimal.h

00001 /*
00002  *   This file is part of the Standard Portable Library (SPL).
00003  *
00004  *   SPL is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   SPL is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with SPL.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 #ifndef _decimal_h
00018 #define _decimal_h
00019 
00020 #include <spl/types.h>
00021 #include <spl/Numeric.h>
00022 #include <spl/String.h>
00023 
00030 #define DECIMAL_MAJIC 0x1234            //< Majic number for ASSERT's in Compare and Convert
00031 
00035 class Decimal : public Numeric
00036 {
00037 protected:
00038         int64 n;
00039 
00040         static const int precision;
00041         static const int64 q;
00042         
00043         inline static Decimal Intern(int64 n) { Decimal dec(0); dec.n = n; return dec; }
00044 
00045         Decimal(int64 intPart, int64 fractPart);
00046         
00047         inline void Init(int64 intPart, int64 fractPart)
00048         {
00049                 n=intPart;
00050                 n*=q;
00051                 n+=fractPart;
00052         }
00053 
00054 public:
00055         static void Initialize();
00056 
00057         Decimal(const int64 i);
00058         Decimal(const int32 i);
00059         Decimal(const int16 i);
00060         Decimal(void);
00061         Decimal(const Decimal& d);
00062         Decimal(const double d);
00063         Decimal(const float d);
00064 
00065         virtual ~Decimal(void);
00066 
00067         Decimal Round();
00068 
00069         Decimal& operator =(const Decimal& d);
00070         Decimal& operator =(const int32 d);
00071         Decimal& operator =(const int64 d);
00072         Decimal& operator =(const float d);
00073         Decimal& operator =(const double d);
00074         Decimal operator +(const Decimal& d) const;
00075         Decimal operator -(const Decimal& d) const;
00076         Decimal operator *(const Decimal& d) const;
00077         Decimal operator /(const Decimal& d) const;
00078 
00079         Decimal operator +=(const Decimal& d);
00080         Decimal operator -=(const Decimal& d);
00081         Decimal operator *=(const Decimal& d);
00082         Decimal operator /=(const Decimal& d);
00083 
00084         bool operator ==(const Decimal&) const;
00085         bool operator !=(const Decimal&) const;
00086         bool operator <(const Decimal&) const;
00087         bool operator <=(const Decimal&) const;
00088         bool operator >(const Decimal&) const;
00089         bool operator >=(const Decimal&) const;
00090 
00091         virtual int ToInt() const;
00092         virtual double ToDouble() const;
00093         virtual StringPtr ToString() const;
00094         inline int64 ToLongInt(void) const { return (int64)(n/q); }
00095 
00096         virtual int32 HashCode() const;
00097 
00098         inline Decimal Abs() { if ( n < 0 ) return Decimal(n * -1); return Decimal(n); }
00099 
00100         inline int64 RawData() const { return n; }
00101         inline static Decimal Parse( const String *str ) { return Parse(*str); }
00102         static Decimal Parse( const String& str );
00103 
00104         bool Equals(Decimal *d) const;
00105         virtual bool Equals(const IComparable *a) const;
00106         virtual int Compare(const IComparable *a) const;
00107         virtual bool Equals( const IComparable& a ) const;
00108         virtual int Compare( const IComparable& a ) const;
00109         virtual int32 MajicNumber() const;
00110 
00111 #ifdef DEBUG
00112         virtual void ValidateMem() const;
00113         virtual void CheckMem() const;
00114 #endif
00115 };
00116 
00117 inline void ValidateType(Decimal& dec)
00118 {
00119 }
00120 
00121 inline void ValidateType(Decimal *dec)
00122 {
00123         ASSERT_PTR(dec);
00124         DEBUG_NOTE_MEM(dec);
00125 }
00126 
00127 REGISTER_TYPEOF( 560, Decimal );
00128 
00131 #endif
00132