-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpe_fraction
259 lines (246 loc) · 7.96 KB
/
pe_fraction
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
#ifndef PE_FRACTION_
#define PE_FRACTION_
#include "pe_base"
#include "pe_int128"
#include "pe_type_traits"
#include "pe_mod"
#include "pe_nt"
namespace pe {
#if PE_HAS_CPP20
#define PE_FRACTION_ARTHMETIC_OPERATION_IMPL(type) \
v + static_cast<type>(1); \
v - static_cast<type>(1); \
v* static_cast<type>(1); \
v / static_cast<type>(1); \
static_cast<type>(1) + v; \
static_cast<type>(1) - v; \
static_cast<type>(1) * v; \
static_cast<type>(1) / v
#define PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(type) \
v += static_cast<type>(1); \
v -= static_cast<type>(1); \
v *= static_cast<type>(1); \
v /= static_cast<type>(1)
template <class T>
concept PeFractionArithmeticOperation = requires(T v) {
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(char);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(unsigned char);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(signed char);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(unsigned int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(signed int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(short int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(unsigned short int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(long int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(signed long int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(unsigned long int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(long long int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(signed long long int);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(unsigned long long int);
#if PE_HAS_INT128
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(int128);
PE_FRACTION_ARTHMETIC_OPERATION_IMPL(uint128);
#endif
v + v;
v - v;
v* v;
v / v;
};
template <class T>
concept PeFractionSelfOperation = requires(T v) {
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(char);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(unsigned char);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(signed char);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(unsigned int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(signed int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(short int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(unsigned short int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(long int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(signed long int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(unsigned long int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(long long int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(signed long long int);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(unsigned long long int);
#if PE_HAS_INT128
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(int128);
PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL(uint128);
#endif
v += v;
v -= v;
v *= v;
v /= v;
++v;
--v;
v++;
v--;
+v;
-v;
};
template <class T>
concept PeFraction = requires(T v) {
requires PeFractionArithmeticOperation<T>;
requires PeFractionSelfOperation<T>;
requires PeComparable<T>;
};
#undef PE_FRACTION_ARTHMETIC_OPERATION_IMPL
#undef PE_FRACTION_SELF_ARTHMETIC_OPERATION_IMPL
#endif
// Forward declaration of gbi version functions.
template <typename GBI>
SL REQUIRES((is_gbi_v<GBI>)) RETURN(GBI) Gcd(GBI m, GBI n);
template <typename GBI>
SL REQUIRES((is_gbi_v<GBI>)) RETURN(GBI) Abs(const GBI& n);
template <typename T>
struct Fraction {
Fraction(const T& u = 0, const T& v = 1) : a(u), b(v) {
if (b != 1 && b != -1) {
T d = Gcd(Abs(a), Abs(b));
if (d > 1) a /= d, b /= d;
}
if (b < 0) b = -b, a = -a;
}
template <typename U>
Fraction(const U& u = 0, const U& v = 1) : Fraction(T(u), T(v)) {}
Fraction(const Fraction& f) {
a = f.a;
b = f.b;
}
Fraction(Fraction&& f) noexcept {
a = std::move(f.a);
b = std::move(f.b);
}
Fraction& operator=(const Fraction& o) {
a = o.a;
b = o.b;
return *this;
}
Fraction& operator=(Fraction&& o) noexcept {
a = std::move(o.a);
b = std::move(o.b);
return *this;
}
#if PE_HAS_CPP20
int operator<=>(const Fraction& o) const {
auto t = a * o.b - b * o.a;
return t < 0 ? -1 : t > 0;
}
int operator==(const Fraction& o) const {
return (this->operator<=>(o)) == 0;
}
int operator!=(const Fraction& o) const {
return (this->operator<=>(o)) != 0;
}
#else
int operator<(const Fraction& o) const { return a * o.b < b * o.a; }
int operator<=(const Fraction& o) const { return a * o.b <= b * o.a; }
int operator>(const Fraction& o) const { return a * o.b > b * o.a; }
int operator>=(const Fraction& o) const { return a * o.b >= b * o.a; }
int operator==(const Fraction& o) const { return a * o.b == b * o.a; }
int operator!=(const Fraction& o) const { return a * o.b != b * o.a; }
#endif
Fraction& operator+=(const Fraction& o) {
return *this = Fraction(a * o.b + o.a * b, b * o.b);
}
Fraction& operator-=(const Fraction& o) {
return *this = Fraction(a * o.b - o.a * b, b * o.b);
}
Fraction& operator*=(const Fraction& o) {
return *this = Fraction(a * o.a, b * o.b);
}
Fraction& operator/=(const Fraction& o) {
return *this = Fraction(a * o.b, b * o.a);
}
Fraction& operator++() {
*this += 1;
return *this;
}
Fraction operator++(int) {
Fraction ret(*this);
*this += 1;
return ret;
}
Fraction& operator--() {
*this -= 1;
return *this;
}
Fraction operator--(int) {
Fraction ret(*this);
*this -= 1;
return ret;
}
double ToDouble() { return 1. * ToFloat<double>(a) / ToFloat<double>(b); }
long double ToLongDouble() {
return static_cast<long double>(1.) * ToFloat<long double>(a) /
ToFloat<long double>(b);
}
T a, b;
};
template <typename T>
Fraction<T> operator+(const Fraction<T>& f) {
return f;
}
template <typename T>
Fraction<T> operator-(const Fraction<T>& f) {
return Fraction<T>(-f.a, f.b);
}
template <typename T>
Fraction<T> operator+(const Fraction<T>& l, const Fraction<T>& r) {
return Fraction(l.a * r.b + r.a * l.b, l.b * r.b);
}
template <typename T, typename U>
Fraction<T> operator+(const Fraction<T>& l, const U& r) {
return l + Fraction<T>(r);
}
template <typename T, typename U>
Fraction<T> operator+(const U& l, const Fraction<T>& r) {
return Fraction<T>(l) + r;
}
template <typename T>
Fraction<T> operator-(const Fraction<T>& l, const Fraction<T>& r) {
return Fraction(l.a * r.b - r.a * l.b, l.b * r.b);
}
template <typename T, typename U>
Fraction<T> operator-(const Fraction<T>& l, const U& r) {
return l - Fraction<T>(r);
}
template <typename T, typename U>
Fraction<T> operator-(const U& l, const Fraction<T>& r) {
return Fraction<T>(l) - r;
}
template <typename T>
Fraction<T> operator*(const Fraction<T>& l, const Fraction<T>& r) {
return Fraction(l.a * r.a, l.b * r.b);
}
template <typename T, typename U>
Fraction<T> operator*(const Fraction<T>& l, const U& r) {
return l * Fraction<T>(r);
}
template <typename T, typename U>
Fraction<T> operator*(const U& l, const Fraction<T>& r) {
return Fraction<T>(l) * r;
}
template <typename T>
Fraction<T> operator/(const Fraction<T>& l, const Fraction<T>& r) {
return Fraction(l.a * r.b, l.b * r.a);
}
template <typename T, typename U>
Fraction<T> operator/(const Fraction<T>& l, const U& r) {
return l / Fraction<T>(r);
}
template <typename T, typename U>
Fraction<T> operator/(const U& l, const Fraction<T>& r) {
return Fraction<T>(l) / r;
}
template <typename T>
std::ostream& operator<<(std::ostream& o, const Fraction<T>& f) {
return o << f.a << "/" << f.b;
}
#if PE_HAS_CPP20
static_assert(PeFraction<Fraction<int64>>);
#if PE_HAS_INT128
static_assert(PeFraction<Fraction<int128>>);
#endif
#endif
} // namespace pe
#endif