decimal.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007,2012 Tommi Maekitalo, Mark Wright
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * As a special exception, you may use this file as part of a free
10  * software library without restriction. Specifically, if other files
11  * instantiate templates or use macros or inline functions from this
12  * file, or you compile this file and link it with other files to
13  * produce an executable, this file does not by itself cause the
14  * resulting executable to be covered by the GNU General Public
15  * License. This exception does not however invalidate any other
16  * reasons why the executable file might be covered by the GNU Library
17  * General Public License.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
27  */
28 
29 #ifndef TNTDB_DECIMAL_H
30 #define TNTDB_DECIMAL_H
31 
32 #include <string>
33 #include <limits>
34 #include <iosfwd>
35 #include <tntdb/config.h>
36 
37 namespace tntdb
38 {
39  class Decimal
40  {
41  public:
42 #ifdef HAVE_LONG_LONG
43  typedef long long LongType;
44 #else
45  typedef long LongType;
46 #endif
47 #ifdef HAVE_UNSIGNED_LONG_LONG
48  typedef unsigned long long UnsignedLongType;
49 #else
50  typedef unsigned long UnsignedLongType;
51 #endif
52 
53  private:
54 
55  // inf: _mantissa is empty, _exponent = numeric_limits<short>::max()
56  // nan: _mantissa is empty, _exponent==0
57  std::string _mantissa; // just '0'-'9'
58  short _exponent;
59  bool _negative;
60 
61  Decimal(const std::string& mantissa, short exponent, bool negative)
62  : _mantissa(mantissa),
63  _exponent(exponent),
64  _negative(negative)
65  { }
66 
67  friend class Parser;
68 
69  LongType _getInteger(LongType min, LongType max) const;
70  UnsignedLongType _getUnsigned(UnsignedLongType max) const;
71 
72  template <typename IntType> IntType _getInteger() const
73  {
74  return _getInteger(std::numeric_limits<IntType>::min(),
75  std::numeric_limits<IntType>::max());
76  }
77 
78  template <typename UnsignedType> UnsignedType _getUnsigned() const
79  { return _getUnsigned(std::numeric_limits<UnsignedLongType>::max()); }
80 
81  void _getInteger(short& ret) const { ret = _getInteger<short>(); }
82  void _getInteger(int& ret) const { ret = _getInteger<int>(); }
83  void _getInteger(long& ret) const { ret = _getInteger<long>(); }
84 #ifdef HAVE_LONG_LONG
85  void _getInteger(long long& ret) const { ret = _getInteger<long long>(); }
86 #endif
87  void _getInteger(unsigned short& ret) const { ret = _getUnsigned<unsigned short>(); }
88  void _getInteger(unsigned int& ret) const { ret = _getUnsigned<unsigned int>(); }
89  void _getInteger(unsigned long& ret) const { ret = _getUnsigned<unsigned long>(); }
90 #ifdef HAVE_UNSIGNED_LONG_LONG
91  void _getInteger(unsigned long long& ret) const { ret = _getUnsigned<unsigned long long>(); }
92 #endif
93 
94  void _setInteger(LongType l, short exponent);
95  void _setUnsigned(UnsignedLongType l, short exponent);
96 
97  public:
98  class Parser;
99 
100  Decimal();
101 
102  explicit Decimal(long double value)
103  { setDouble(value); }
104 
105  explicit Decimal(const std::string& value);
106 
107  explicit Decimal(long mantissa, short exponent)
108  { setInteger(mantissa, exponent); }
109 
110  static Decimal infinity()
111  { return Decimal(std::string(), std::numeric_limits<short>::max(), false); }
112 
113  static Decimal nan()
114  { return Decimal(std::string(), 0, false); }
115 
116  const std::string& mantissa() const
117  { return _mantissa; }
118 
119  short exponent() const
120  { return _exponent; }
121 
122  bool negative() const
123  { return _negative; }
124 
125  bool isInfinity(bool positiveInfinity = true) const
126  { return _negative != positiveInfinity && _mantissa.empty() && _exponent == std::numeric_limits<short>::max(); }
127 
128  bool isPositiveInfinity() const
129  { return isInfinity(true); }
130 
131  bool isNegativeInfinity() const
132  { return isInfinity(false); }
133 
134  bool isNaN() const
135  { return _mantissa.empty() && _exponent == 0; }
136 
137  bool isZero() const
138  { return _mantissa == "0"; }
139 
140  void setDouble(long double value);
141 
142  long double getDouble() const;
143 
144  void setInteger(short l, short exponent = 0) { _setInteger(l, exponent); }
145  void setInteger(int l, short exponent = 0) { _setInteger(l, exponent); }
146  void setInteger(long l, short exponent = 0) { _setInteger(l, exponent); }
147  void setInteger(long long l, short exponent = 0) { _setInteger(l, exponent); }
148  void setInteger(unsigned short l, short exponent = 0) { _setUnsigned(l, exponent); }
149  void setInteger(unsigned int l, short exponent = 0) { _setUnsigned(l, exponent); }
150  void setInteger(unsigned long l, short exponent = 0) { _setUnsigned(l, exponent); }
151  void setInteger(unsigned long long l, short exponent = 0) { _setUnsigned(l, exponent); }
152 
153  template <typename IntType>
154  IntType getInteger() const
155  {
156  IntType ret;
157  _getInteger(ret);
158  return ret;
159  }
160 
161  std::string toString() const;
162  std::string toStringSci() const;
163  std::string toStringFix() const;
164 
166  { return Decimal(_mantissa, _exponent, !_negative); }
167 
168  bool operator== (const Decimal& other) const
169  { return !isNaN() && _mantissa == other._mantissa && _exponent == other._exponent && _negative == other._negative; }
170 
171  bool operator!= (const Decimal& other) const
172  { return !(*this == other); }
173 
174  bool operator< (const Decimal& other) const;
175 
176  bool operator> (const Decimal& other) const
177  { return other < *this; }
178 
179  bool operator<= (const Decimal& other) const
180  { return !(other < *this); }
181 
182  bool operator>= (const Decimal& other) const
183  { return !(other > *this); }
184  };
185 
186  std::istream& operator>> (std::istream& in, Decimal& dec);
187  std::ostream& operator<< (std::ostream& out, const Decimal& dec);
188 }
189 
190 #endif // TNTDB_DECIMAL_H
191