-
Notifications
You must be signed in to change notification settings - Fork 17
/
_FixedPoint.hpp
142 lines (120 loc) · 3.46 KB
/
_FixedPoint.hpp
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
#pragma once
#include <cstddef>
#include <climits>
#include <cmath>
#include <cassert>
//#include <string>
template <int kScale>
class FixedPoint {
static_assert(kScale >= 1, "FixedPoint requires kScale >= 1");
int digits;
public:
FixedPoint() = default;
FixedPoint(int base, int fract = 0)
: digits(base * kScale + fract)
{
if (!(fract < kScale && fract >= 0))
qWarning() << base << fract << kScale;
assert(fract < kScale && fract >= 0);
assert(base < INT_MAX / kScale);
}
FixedPoint(double value)
: FixedPoint(static_cast<int>(std::floor(value)),
static_cast<int>((value - std::floor(value)) * kScale))
{
}
int base() const {
return digits >= 0 ? digits / kScale : digits / kScale - 1;
}
int fract() const {
return digits >= 0 ? digits % kScale : kScale - digits % kScale;
}
int const &rawDigits() const {
return digits;
}
int &rawDigits() {
return digits;
}
operator double() const {
// assert(base() * kScale + fract() == digits);
return (double)digits / kScale;
}
FixedPoint &operator+=(FixedPoint const &that) {
digits += that.digits;
return *this;
}
FixedPoint &operator-=(FixedPoint const &that) {
digits -= that.digits;
return *this;
}
FixedPoint &operator*=(FixedPoint const &that) {
digits *= that.digits;
digits /= kScale;
return *this;
}
FixedPoint &operator/=(FixedPoint const &that) {
digits *= kScale;
digits /= that.digits;
return *this;
}
FixedPoint operator+(FixedPoint const &that) const {
FixedPoint tmp = *this;
tmp += that;
return tmp;
}
FixedPoint operator-(FixedPoint const &that) const {
FixedPoint tmp = *this;
tmp -= that;
return tmp;
}
FixedPoint operator*(FixedPoint const &that) const {
FixedPoint tmp = *this;
tmp *= that;
return tmp;
}
FixedPoint operator/(FixedPoint const &that) const {
FixedPoint tmp = *this;
tmp /= that;
return tmp;
}
FixedPoint operator+() const {
return *this;
}
FixedPoint operator-() const {
FixedPoint tmp;
tmp.digits = -digits;
return tmp;
}
/*static FixedPoint from_string(std::string str, bool *ok = nullptr, int strBase = 10) {
if (str.isEmpty()) {
if (ok) *ok = false;
return FixedPoint();
}
int sign = 1;
if (str.front() == '-' && str.size() >= 2) {
str = str.substr(1);
sign = -1;
}
if (ok) *ok = true;
size_t dot = str.find('.');
if (dot != -1) {
int base = dot != 0 ?
std::stoi(str.substr(0, dot), nullptr, strBase) : 0;
unsigned int fract = dot + 1 != str.size() ?
std::stoul(str.substr(dot + 1)) : 0;
if (kScale == 1) {
fract = 0;
} else {
while (fract && fract < kScale / strBase) {
fract *= 10;
}
if (fract >= kScale)
fract /= strBase;
}
return FixedPoint(sign * base, fract);
} else {
int base = std::stoi(str.substr(0, dot), nullptr, strBase);
return FixedPoint(sign * base);
}
}*/
};