Zero  0.1.0
decimal.h
Go to the documentation of this file.
1 /* -*- mode:C++; c-basic-offset:4 -*-
2  Shore-kits -- Benchmark implementations for Shore-MT
3 
4  Copyright (c) 2007-2009
5  Data Intensive Applications and Systems Labaratory (DIAS)
6  Ecole Polytechnique Federale de Lausanne
7 
8  All Rights Reserved.
9 
10  Permission to use, copy, modify and distribute this software and
11  its documentation is hereby granted, provided that both the
12  copyright notice and this permission notice appear in all copies of
13  the software, derivative works or modified versions, and any
14  portions thereof, and that both notices appear in supporting
15  documentation.
16 
17  This code is distributed in the hope that it will be useful, but
18  WITHOUT ANY WARRANTY; without even the implied warranty of
19  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. THE AUTHORS
20  DISCLAIM ANY LIABILITY OF ANY KIND FOR ANY DAMAGES WHATSOEVER
21  RESULTING FROM THE USE OF THIS SOFTWARE.
22 */
23 
24 #ifndef __DECIMAL_H
25 #define __DECIMAL_H
26 
27 #include <cstdint>
28 
32 class decimal {
33  int64_t _value;
34 
35  explicit decimal(int64_t value)
36  : _value(value) {}
37 
38 public:
40  : _value(0) {}
41 
42  decimal(double value)
43  : _value((int64_t)(value * 100)) {}
44 
45  decimal(int value)
46  : _value((int64_t)(value * 100)) {}
47 
48  decimal& operator+=(decimal const& other) {
49  _value += other._value;
50  return *this;
51  }
52 
53  decimal& operator-=(decimal const& other) {
54  _value -= other._value;
55  return *this;
56  }
57 
58  decimal operator*=(decimal const& other) {
59  _value = (_value * other._value + 50) / 100;
60  return *this;
61  }
62 
63  decimal operator/=(decimal const& other) {
64  _value = (100 * _value + 50) / other._value;
65  return *this;
66  }
67 
68  decimal operator+(decimal const& other) const {
69  return decimal(*this) += other;
70  }
71 
72  decimal operator-(decimal const& other) const {
73  return decimal(*this) -= other;
74  }
75 
76  decimal operator*(decimal const& other) const {
77  return decimal(*this) *= other;
78  }
79 
80  decimal operator/(decimal const& other) const {
81  return decimal(*this) /= other;
82  }
83 
85  _value += 100;
86  return *this;
87  }
88 
90  decimal old = *this;
91  ++*this;
92  return old;
93  }
94 
96  _value -= 100;
97  return *this;
98  }
99 
101  decimal old = *this;
102  --*this;
103  return old;
104  }
105 
106  double to_double() const {
107  return ((double)_value / (double)100);
108  }
109 
110  long long to_long() const {
111  return (_value + 50) / 100;
112  }
113 
114  int to_int() const {
115  return (int)to_long();
116  }
117 
118  bool operator<(decimal const& other) const {
119  return _value < other._value;
120  }
121 
122  bool operator>(decimal const& other) const {
123  return _value > other._value;
124  }
125 
126  bool operator==(decimal const& other) const {
127  return _value == other._value;
128  }
129 
130  bool operator<=(decimal const& other) const {
131  return _value <= other._value;
132  }
133 
134  bool operator>=(decimal const& other) const {
135  return _value >= other._value;
136  }
137 
138  bool operator!=(decimal const& other) const {
139  return _value != other._value;
140  }
141 };
142 
143 inline decimal operator+(int a, decimal const& b) {
144  return decimal(a) + b;
145 }
146 
147 inline decimal operator-(int a, decimal const& b) {
148  return decimal(a) - b;
149 }
150 
151 inline decimal operator*(int a, decimal const& b) {
152  return decimal(a) * b;
153 }
154 
155 inline decimal operator/(int a, decimal const& b) {
156  return decimal(a) / b;
157 }
158 
159 inline decimal operator+(double a, decimal const& b) {
160  return decimal(a) + b;
161 }
162 
163 inline decimal operator-(double a, decimal const& b) {
164  return decimal(a) - b;
165 }
166 
167 inline decimal operator*(double a, decimal const& b) {
168  return decimal(a) * b;
169 }
170 
171 inline decimal operator/(double a, decimal const& b) {
172  return decimal(a) / b;
173 }
174 
175 #endif // __DECIMAL_H
int64_t _value
Definition: decimal.h:33
decimal operator+(decimal const &other) const
Definition: decimal.h:68
Definition: decimal.h:32
decimal(int value)
Definition: decimal.h:45
decimal operator--(int)
Definition: decimal.h:100
decimal operator++(int)
Definition: decimal.h:89
decimal & operator+=(decimal const &other)
Definition: decimal.h:48
bool operator>=(decimal const &other) const
Definition: decimal.h:134
decimal operator/=(decimal const &other)
Definition: decimal.h:63
bool operator<=(decimal const &other) const
Definition: decimal.h:130
decimal(double value)
Definition: decimal.h:42
bool operator>(decimal const &other) const
Definition: decimal.h:122
decimal(int64_t value)
Definition: decimal.h:35
decimal & operator-=(decimal const &other)
Definition: decimal.h:53
int to_int() const
Definition: decimal.h:114
long long to_long() const
Definition: decimal.h:110
decimal operator*(decimal const &other) const
Definition: decimal.h:76
decimal & operator++()
Definition: decimal.h:84
decimal operator-(decimal const &other) const
Definition: decimal.h:72
decimal operator*=(decimal const &other)
Definition: decimal.h:58
double to_double() const
Definition: decimal.h:106
decimal()
Definition: decimal.h:39
bool operator==(decimal const &other) const
Definition: decimal.h:126
decimal operator/(decimal const &other) const
Definition: decimal.h:80
bool operator<(decimal const &other) const
Definition: decimal.h:118
decimal & operator--()
Definition: decimal.h:95
bool operator!=(decimal const &other) const
Definition: decimal.h:138