-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathFixed.h
149 lines (127 loc) · 4.14 KB
/
Fixed.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
#ifndef __FIXED__
#define __FIXED__
#include <math.h>
#include "Utilities.h"
namespace FixedPriceOrderBook {
//-----------------------------------------------------------------------------
class Fixed {
public:
//------------------------------------------------------------
Fixed(double input, uint32_t precision = 5) {
precision_ = pow(10, precision);
whole_part_ = static_cast<int64_t>(input);
fract_part_ = static_cast<int64_t>(round((input - whole_part_) * precision_));
// fprintf(stderr, "DOUBLE CTOR: Whole part int: %ld. Fract part int: %ld\n", whole_part_, fract_part_);
}
Fixed(int32_t input, uint32_t precision = 5) {
precision_ = pow(10, precision);
whole_part_ = input;
fract_part_ = 0;
// fprintf(stderr, "INT CTOR: Whole part int: %ld. Fract part int: %ld\n", whole_part_, fract_part_);
}
//------------------------------------------------------------
Fixed(const Fixed &rhs)
: whole_part_(rhs.whole_part_)
, fract_part_(rhs.fract_part_)
, precision_(rhs.precision_) { }
//------------------------------------------------------------
double getDouble() const {
double price = 0.0;
price += whole_part_;
price += static_cast<double>(fract_part_ / static_cast<double>(precision_));
// DB("getDouble returning double: %.5f\n", price);
return price;
}
//------------------------------------------------------------
bool operator==(const Fixed &rhs) const {
return whole_part_ != rhs.whole_part_ ? false :
fract_part_ != rhs.fract_part_ ? false :
true;
}
//------------------------------------------------------------
bool operator !=(const Fixed &rhs) const {
return !operator==(rhs);
}
//------------------------------------------------------------
bool operator <(const Fixed &rhs) const {
if (whole_part_ < rhs.whole_part_) {
return true;
}
if (whole_part_ == rhs.whole_part_) {
if (fract_part_ < rhs.fract_part_) {
return true;
}
}
return false;
}
//------------------------------------------------------------
bool operator >(const Fixed &rhs) const {
return !operator<(rhs);
}
//------------------------------------------------------------
bool operator <=(const Fixed &rhs) const {
return operator<(rhs) ? true : operator==(rhs);
}
//------------------------------------------------------------
bool operator >=(const Fixed &rhs) const {
return operator>(rhs) ? true : operator==(rhs);
}
//------------------------------------------------------------
Fixed &operator =(const Fixed &rhs) {
if (this == &rhs) {
return *this;
}
whole_part_ = rhs.whole_part_;
fract_part_ = rhs.fract_part_;
precision_ = rhs.precision_;
return *this;
}
//------------------------------------------------------------
Fixed &operator +=(const Fixed &rhs) {
whole_part_ += rhs.whole_part_;
fract_part_ += rhs.fract_part_;
return *this;
}
//------------------------------------------------------------
// Duplicating to avoid temporary on stack for negation of const rhs
Fixed &operator -=(const Fixed &rhs) {
whole_part_ -= rhs.whole_part_;
fract_part_ -= rhs.fract_part_;
return *this;
}
//------------------------------------------------------------
void operator *=(const Fixed &rhs) {
double t = rhs.getDouble();
whole_part_ *= t;
fract_part_ *= t;
while (fract_part_ > (int64_t)precision_) {
whole_part_ += 1;
fract_part_ -= precision_;
}
}
private:
int64_t whole_part_;
int64_t fract_part_;
uint64_t precision_;
};
//------------------------------------------------------------
Fixed operator+(const Fixed &lhs, const Fixed &rhs) {
Fixed r(lhs);
r += rhs;
return r;
}
Fixed operator-(const Fixed &lhs, const Fixed &rhs) {
Fixed r(lhs);
r -= rhs;
return r;
}
class FixedComparer {
public:
bool operator()(const Fixed &lhs, const Fixed &rhs) {
if (lhs < rhs)
return true;
return false;
}
};
}
#endif