-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprice.h
367 lines (305 loc) · 7.19 KB
/
price.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
#pragma once
#include <stdint.h>
#include <math.h>
#include <limits>
#include <exception>
struct managed
{
typedef std::exception price_overflow_exc;
static int64_t checked_addition(const int64_t& lhs, const int64_t& rhs);
static int64_t checked_subtraction(const int64_t& lhs, const int64_t& rhs);
static int64_t checked_mult(const int64_t& lhs, const long double& rhs);
static int64_t checked_assign(const long double& rhs);
};
struct unmanaged {};
template<typename M>
struct price_t
{
int64_t m_price;
unsigned short m_scale;
price_t()
: m_price(0)
, m_scale(0)
{}
price_t(int64_t p, unsigned short s)
: m_price(p)
, m_scale(s)
{}
explicit price_t(double d)
{
assign(d);
}
explicit price_t(int64_t i64)
: m_price(i64)
, m_scale(0)
{}
price_t(const price_t& p)
{
m_price = p.m_price;
m_scale = p.m_scale;
}
explicit price_t(unsigned int value)
: m_price(static_cast<int64_t>(value))
, m_scale(0)
{}
explicit price_t(int value)
: m_price(static_cast<int64_t>(value))
, m_scale(0)
{}
explicit price_t(float value)
{
assign(value);
}
price_t & operator=(const price_t &rhs)
{
if (&rhs != this)
{
m_price = rhs.m_price;
m_scale = rhs.m_scale;
}
return *this;
}
price_t & operator=(int64_t rhs)
{
m_price = static_cast<int64_t>(rhs * pow(10, m_scale));
return *this;
}
price_t & operator=(int rhs)
{
m_price = static_cast<int64_t>(rhs * pow(10, m_scale));
return *this;
}
price_t & operator=(double rhs)
{
unsigned short s(m_scale);
assign(rhs);
change_scale(s);
return *this;
}
// math operators
const price_t operator + (const price_t& p) const
{
price_t<M> res(*this);
res.m_price = static_cast<int64_t>((res.get_integral() + p.get_integral()) * pow(10, res.m_scale));
res.m_price += res.m_scale < p.m_scale
? get_fractional(p.m_scale - res.m_scale) + p.get_fractional()
: get_fractional() + p.get_fractional(res.m_scale - p.m_scale);
return res;
}
price_t& operator += (const price_t& p)
{
*this = this->operator+(p);
return *this;
}
const price_t operator+() const
{
return *this;
}
const price_t operator-() const
{
price_t res = *this;
res.m_price = -res.m_price;
return res;
}
const price_t operator - (const price_t& p) const
{
price_t res(*this);
res.m_price = static_cast<int64_t>((res.get_integral() - p.get_integral()) * pow(10, res.m_scale));
res.m_price += res.m_scale < p.m_scale
? get_fractional(p.m_scale - res.m_scale) - p.get_fractional()
: get_fractional() - p.get_fractional(res.m_scale - p.m_scale);
return res;
}
price_t& operator -= (const price_t& p)
{
*this = this->operator-(p);
return *this;
}
const price_t operator * (const double& p) const // in-scale multiplication
{
price_t res(*this);
unsigned short s(res.m_scale);
res.assign(res.get_double() * p);
res.change_scale(s);
return res;
}
price_t& operator *= (const double& p)
{
*this = this->operator*(p);
return *this;
}
const price_t operator / (const double& p) const // in-scale division
{
price_t res(*this);
unsigned short s(res.m_scale);
res.assign(res.get_double() / p);
res.change_scale(s);
return res;
}
price_t& operator /= (const double& p)
{
*this = this->operator/(p);
return *this;
}
// logic operators
bool operator > (const price_t& p) const
{
if (get_integral() <= p.get_integral())
{
if (m_scale <= p.m_scale)
{
return (get_fractional(p.m_scale - m_scale) > p.get_fractional());
}
else
{
return (get_fractional() > p.get_fractional(m_scale - p.m_scale));
}
}
return true;
}
bool operator >= (const price_t& p) const
{
if (get_integral() < p.get_integral())
{
if (m_scale <= p.m_scale)
{
return (get_fractional(p.m_scale - m_scale) >= p.get_fractional());
}
else
{
return (get_fractional() >= p.get_fractional(m_scale - p.m_scale));
}
}
return true;
}
bool operator < (const price_t& p) const
{
return !this->operator >=(p);
}
bool operator <= (const price_t& p) const
{
return !this->operator > (p);
}
bool operator == (const price_t& p) const
{
if(get_integral() == p.get_integral())
{
if (m_scale <= p.m_scale)
{
return (get_fractional(p.m_scale - m_scale) == p.get_fractional());
}
else
{
return (get_fractional() == p.get_fractional(m_scale - p.m_scale));
}
}
return false;
}
bool operator != (const price_t& p) const
{
return !this->operator==(p);
}
bool operator == (const double& d) const
{
return this->operator==(price_t(d));
}
bool operator != (const double& d) const
{
return this->operator==(price_t(d));
}
// cast
long double get_double() const
{
return m_price / pow(10, m_scale);
}
// other
void mult(const double& d)
{
assign(get_double() * d);
}
void div(const double& d)
{
assign(get_double() / d);
}
private:
void assign(const long double& d)
{
m_scale = GetScale(d);
m_price = static_cast<int64_t>(d * pow(10, m_scale));
}
int64_t get_integral() const
{
return static_cast<int64_t>(m_price / pow(10, m_scale));
}
int64_t get_fractional(unsigned short s = 0) const
{
return static_cast<int64_t>(m_price % static_cast<uint64_t>(pow(10, m_scale)) * pow(10, s));
}
void change_scale(unsigned short s)
{
if (s == m_scale)
return;
if (s > m_scale)
{
m_price *= pow(10, s - m_scale);
}
else
{
m_price = std::llround(m_price / pow(10, m_scale - s));
}
m_scale = s;
}
private:
unsigned short GetScale(long double d, const long double& prec = 1e-6)
{
unsigned short s(1);
while ((d - static_cast<int64_t>(d)) > prec && s++)
d *= 10;
return s - 1;
}
};
template<>
const price_t<managed> price_t<managed>::operator+(const price_t<managed>& p) const
{
price_t<managed> res(*this);
res.m_price = managed::checked_addition(res.get_integral(), p.get_integral());
res.m_price = managed::checked_mult(res.m_price, pow(10, res.m_scale));
res.m_price = managed::checked_addition(res.m_price, res.m_scale < p.m_scale
? managed::checked_addition(get_fractional(p.m_scale - res.m_scale), p.get_fractional())
: managed::checked_addition(get_fractional(), p.get_fractional(res.m_scale - p.m_scale)));
return res;
}
template<>
const price_t<managed> price_t<managed>::operator-(const price_t<managed>& p) const
{
price_t<managed> res(*this);
res.m_price = managed::checked_subtraction(res.get_integral(), p.get_integral());
res.m_price = managed::checked_mult(res.m_price, pow(10, res.m_scale));
res.m_price = managed::checked_subtraction(res.m_price, res.m_scale < p.m_scale
? managed::checked_subtraction(get_fractional(p.m_scale - res.m_scale), p.get_fractional())
: managed::checked_subtraction(get_fractional(), p.get_fractional(res.m_scale - p.m_scale)));
return res;
}
template<>
void price_t<managed>::assign(const long double& d)
{
m_scale = GetScale(d);
m_price = managed::checked_assign(d * pow(10, m_scale));
}
template<>
void price_t<managed>::change_scale(unsigned short s)
{
if (s == m_scale)
return;
if (s > m_scale)
{
m_price = managed::checked_assign(pow(10, s - m_scale) * m_price);
}
else
{
m_price = std::llround(m_price / pow(10, m_scale - s));
}
m_scale = s;
}
typedef price_t<unmanaged> TPrice;
typedef price_t<managed> TPriceManaged;