1 | /* |
2 | * Legal Notice |
3 | * |
4 | * This document and associated source code (the "Work") is a part of a |
5 | * benchmark specification maintained by the TPC. |
6 | * |
7 | * The TPC reserves all right, title, and interest to the Work as provided |
8 | * under U.S. and international laws, including without limitation all patent |
9 | * and trademark rights therein. |
10 | * |
11 | * No Warranty |
12 | * |
13 | * 1.1 TO THE MAXIMUM EXTENT PERMITTED BY APPLICABLE LAW, THE INFORMATION |
14 | * CONTAINED HEREIN IS PROVIDED "AS IS" AND WITH ALL FAULTS, AND THE |
15 | * AUTHORS AND DEVELOPERS OF THE WORK HEREBY DISCLAIM ALL OTHER |
16 | * WARRANTIES AND CONDITIONS, EITHER EXPRESS, IMPLIED OR STATUTORY, |
17 | * INCLUDING, BUT NOT LIMITED TO, ANY (IF ANY) IMPLIED WARRANTIES, |
18 | * DUTIES OR CONDITIONS OF MERCHANTABILITY, OF FITNESS FOR A PARTICULAR |
19 | * PURPOSE, OF ACCURACY OR COMPLETENESS OF RESPONSES, OF RESULTS, OF |
20 | * WORKMANLIKE EFFORT, OF LACK OF VIRUSES, AND OF LACK OF NEGLIGENCE. |
21 | * ALSO, THERE IS NO WARRANTY OR CONDITION OF TITLE, QUIET ENJOYMENT, |
22 | * QUIET POSSESSION, CORRESPONDENCE TO DESCRIPTION OR NON-INFRINGEMENT |
23 | * WITH REGARD TO THE WORK. |
24 | * 1.2 IN NO EVENT WILL ANY AUTHOR OR DEVELOPER OF THE WORK BE LIABLE TO |
25 | * ANY OTHER PARTY FOR ANY DAMAGES, INCLUDING BUT NOT LIMITED TO THE |
26 | * COST OF PROCURING SUBSTITUTE GOODS OR SERVICES, LOST PROFITS, LOSS |
27 | * OF USE, LOSS OF DATA, OR ANY INCIDENTAL, CONSEQUENTIAL, DIRECT, |
28 | * INDIRECT, OR SPECIAL DAMAGES WHETHER UNDER CONTRACT, TORT, WARRANTY, |
29 | * OR OTHERWISE, ARISING IN ANY WAY OUT OF THIS OR ANY OTHER AGREEMENT |
30 | * RELATING TO THE WORK, WHETHER OR NOT SUCH AUTHOR OR DEVELOPER HAD |
31 | * ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. |
32 | * |
33 | * Contributors |
34 | * - Sergey Vasilevskiy |
35 | */ |
36 | |
37 | /****************************************************************************** |
38 | * Description: Money type that keeps all calculations in integer |
39 | * number of cents. Needed for consistency of initial |
40 | * database population. |
41 | ******************************************************************************/ |
42 | |
43 | #ifndef MONEY_H |
44 | #define MONEY_H |
45 | |
46 | #include "EGenStandardTypes.h" |
47 | |
48 | namespace TPCE { |
49 | |
50 | class CMoney { |
51 | INT64 m_iAmountInCents; // dollar amount * 100 |
52 | |
53 | // Define binary operators when CMoney is the right operand |
54 | // |
55 | friend CMoney operator*(int l_i, CMoney r_m); |
56 | friend CMoney operator*(double l_f, CMoney r_m); |
57 | friend double operator/(double l_f, CMoney r_m); |
58 | |
59 | public: |
60 | // Default constructor - initialize to $0 |
61 | // |
62 | CMoney() : m_iAmountInCents(0) { |
63 | } |
64 | |
65 | // Initialize from another CMoney |
66 | // |
67 | CMoney(CMoney *m) : m_iAmountInCents(m->m_iAmountInCents) { |
68 | } |
69 | |
70 | // Initialize CMoney from double |
71 | // |
72 | CMoney(double fAmount) |
73 | : m_iAmountInCents((INT64)(100.0 * fAmount + 0.5)) // round floating-point number correctly |
74 | { |
75 | } |
76 | |
77 | // Return amount in integer dollars and fractional cents e.g. $123.99 |
78 | // |
79 | double DollarAmount() { |
80 | return m_iAmountInCents / 100.0; |
81 | } |
82 | |
83 | // Return amount in integer cents e.g. 12399 |
84 | INT64 CentsAmount() { |
85 | return m_iAmountInCents; |
86 | } |
87 | |
88 | // Define arithmetic operations on CMoney and CMoney |
89 | // |
90 | CMoney operator+(const CMoney &m) { |
91 | CMoney ret(this); |
92 | |
93 | ret.m_iAmountInCents += m.m_iAmountInCents; |
94 | |
95 | return ret; |
96 | } |
97 | |
98 | CMoney &operator+=(const CMoney &m) { |
99 | m_iAmountInCents += m.m_iAmountInCents; |
100 | |
101 | return *this; |
102 | } |
103 | |
104 | CMoney operator-(const CMoney &m) { |
105 | CMoney ret(this); |
106 | |
107 | ret.m_iAmountInCents -= m.m_iAmountInCents; |
108 | |
109 | return ret; |
110 | } |
111 | |
112 | CMoney &operator-=(const CMoney &m) { |
113 | m_iAmountInCents -= m.m_iAmountInCents; |
114 | |
115 | return *this; |
116 | } |
117 | |
118 | CMoney operator*(const CMoney &m) { |
119 | CMoney ret(this); |
120 | |
121 | ret.m_iAmountInCents *= m.m_iAmountInCents; |
122 | |
123 | return ret; |
124 | } |
125 | |
126 | CMoney operator/(const CMoney &m) { |
127 | CMoney ret(this); |
128 | |
129 | ret.m_iAmountInCents /= m.m_iAmountInCents; |
130 | |
131 | return ret; |
132 | } |
133 | |
134 | // Define arithmetic operations on CMoney and int |
135 | // |
136 | CMoney operator*(int i) { |
137 | CMoney ret(this); |
138 | |
139 | ret.m_iAmountInCents *= i; |
140 | |
141 | return ret; |
142 | } |
143 | |
144 | // Define arithmetic operations on CMoney and double |
145 | // |
146 | CMoney operator+(double f) { |
147 | CMoney ret(this); |
148 | |
149 | ret.m_iAmountInCents += (INT64)(100.0 * f + 0.5); |
150 | |
151 | return ret; |
152 | } |
153 | |
154 | CMoney operator-(double f) { |
155 | CMoney ret(this); |
156 | |
157 | ret.m_iAmountInCents -= (INT64)(100.0 * f + 0.5); |
158 | |
159 | return ret; |
160 | } |
161 | |
162 | CMoney &operator-=(double f) { |
163 | m_iAmountInCents -= (INT64)(100.0 * f + 0.5); |
164 | |
165 | return *this; |
166 | } |
167 | |
168 | CMoney operator*(double f) { |
169 | CMoney ret(this); |
170 | |
171 | // Do a trick for correct rounding. Can't use ceil or floor functions |
172 | // because they do not round properly (e.g. down when < 0.5, up when >= |
173 | // 0.5). |
174 | // |
175 | if (ret.m_iAmountInCents > 0) { |
176 | ret.m_iAmountInCents = (INT64)(ret.m_iAmountInCents * f + 0.5); |
177 | } else { |
178 | ret.m_iAmountInCents = (INT64)(ret.m_iAmountInCents * f - 0.5); |
179 | } |
180 | |
181 | return ret; |
182 | } |
183 | |
184 | CMoney operator/(double f) { |
185 | CMoney ret(this); |
186 | |
187 | if (ret.m_iAmountInCents > 0) { |
188 | ret.m_iAmountInCents = (INT64)(ret.m_iAmountInCents / f + 0.5); |
189 | } else { |
190 | ret.m_iAmountInCents = (INT64)(ret.m_iAmountInCents / f - 0.5); |
191 | } |
192 | |
193 | return ret; |
194 | } |
195 | |
196 | // Assignment of a double (presumed fractional dollar amount e.g. in the |
197 | // form $123.89) |
198 | // |
199 | CMoney &operator=(double f) { |
200 | m_iAmountInCents = (INT64)(100.0 * f + 0.5); |
201 | |
202 | return *this; |
203 | } |
204 | |
205 | // Comparison operators |
206 | // |
207 | bool operator==(const CMoney &m) { |
208 | return m_iAmountInCents == m.m_iAmountInCents; |
209 | } |
210 | |
211 | bool operator>(const CMoney &m) { |
212 | return m_iAmountInCents > m.m_iAmountInCents; |
213 | } |
214 | bool operator>=(const CMoney &m) { |
215 | return m_iAmountInCents >= m.m_iAmountInCents; |
216 | } |
217 | |
218 | bool operator<(const CMoney &m) { |
219 | return m_iAmountInCents < m.m_iAmountInCents; |
220 | } |
221 | bool operator<=(const CMoney &m) { |
222 | return m_iAmountInCents <= m.m_iAmountInCents; |
223 | } |
224 | }; |
225 | |
226 | } // namespace TPCE |
227 | |
228 | #endif // #ifndef MONEY_H |
229 | |