-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathfixedpoint.h
More file actions
139 lines (105 loc) · 4.8 KB
/
Copy pathfixedpoint.h
File metadata and controls
139 lines (105 loc) · 4.8 KB
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
#ifndef FIXEDPOINT_H_
#define FIXEDPOINT_H_
#include <iostream>
#include "types.h"
template<unsigned SCALE, typename T = Number>
struct Fixed
{
T value;
Fixed(T n): value(n*SCALE) { }
Fixed(int n): value(n*SCALE) { }
Fixed(double n): value(n*SCALE+0.5) { }
template<typename U>
explicit Fixed(Fixed<SCALE, U> f): value(f.value) { }
static Fixed from_raw(T n) { Fixed f(T(0)); f.value = n; return f; }
Fixed &operator+=(Fixed f) { value += f.value; return *this; }
Fixed &operator+=(T n) { value += n*SCALE; return *this; }
Fixed &operator-=(Fixed f) { value -= f.value; return *this; }
Fixed &operator-=(T n) { value -= n*SCALE; return *this; }
Fixed &operator*=(T n) { value *= n; return *this; }
Fixed &operator/=(T n) { value /= n; return *this; }
T floor() const { return value/SCALE; }
T round() const { return (value+SCALE/2)/SCALE; }
double to_real() const { return static_cast<double>(value)/SCALE; }
template<unsigned TO_SCALE>
Fixed<TO_SCALE, T> rescale() const { return Fixed<TO_SCALE, T>::from_raw((value*TO_SCALE+SCALE/2)/SCALE); }
};
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator+(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return Fixed<SCALE, T>::from_raw(f1.value+f2.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator-(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return Fixed<SCALE, T>::from_raw(f1.value-f2.value); }
template<unsigned SCALE1, unsigned SCALE2, typename T>
inline Fixed<SCALE1*SCALE2, T> operator*(Fixed<SCALE1, T> f1, Fixed<SCALE2, T> f2)
{ return Fixed<SCALE1*SCALE2, T>::from_raw(f1.value*f2.value); }
template<unsigned SCALE1, unsigned SCALE2, typename T>
inline Fixed<SCALE1/SCALE2, T> operator/(Fixed<SCALE1, T> f1, Fixed<SCALE2, T> f2)
{
static_assert(SCALE1%SCALE2==0, "Division of incompatible fixed-point numbers");
return Fixed<SCALE1/SCALE2, T>::from_raw(f1.value/f2.value);
}
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator+(Fixed<SCALE, T> f, T n)
{ return Fixed<SCALE, T>::from_raw(f.value+n*SCALE); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator+(T n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*SCALE+f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator-(Fixed<SCALE, T> f, T n)
{ return Fixed<SCALE, T>::from_raw(f.value-n*SCALE); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator-(T n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*SCALE-f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator*(Fixed<SCALE, T> f, T n)
{ return Fixed<SCALE, T>::from_raw(f.value*n); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator*(T n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator/(Fixed<SCALE, T> f, T n)
{ return Fixed<SCALE, T>::from_raw(f.value/n); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator+(Fixed<SCALE, T> f, int n)
{ return Fixed<SCALE, T>::from_raw(f.value+n*SCALE); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator+(int n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*SCALE+f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator-(Fixed<SCALE, T> f, int n)
{ return Fixed<SCALE, T>::from_raw(f.value-n*SCALE); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator-(int n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*SCALE-f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator*(Fixed<SCALE, T> f, int n)
{ return Fixed<SCALE, T>::from_raw(f.value*n); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator*(int n, Fixed<SCALE, T> f)
{ return Fixed<SCALE, T>::from_raw(n*f.value); }
template<unsigned SCALE, typename T>
inline Fixed<SCALE, T> operator/(Fixed<SCALE, T> f, int n)
{ return Fixed<SCALE, T>::from_raw(f.value/n); }
template<unsigned SCALE, typename T>
inline bool operator==(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value==f2.value; }
template<unsigned SCALE, typename T>
inline bool operator!=(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value!=f2.value; }
template<unsigned SCALE, typename T>
inline bool operator<(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value<f2.value; }
template<unsigned SCALE, typename T>
inline bool operator<=(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value<=f2.value; }
template<unsigned SCALE, typename T>
inline bool operator>(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value>f2.value; }
template<unsigned SCALE, typename T>
inline bool operator>=(Fixed<SCALE, T> f1, Fixed<SCALE, T> f2)
{ return f1.value>=f2.value; }
template<unsigned SCALE, typename T>
inline std::ostream &operator<<(std::ostream &os, Fixed<SCALE, T> f)
{ os << f.round(); return os; }
#endif