-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfraction.h
More file actions
365 lines (322 loc) · 11.7 KB
/
fraction.h
File metadata and controls
365 lines (322 loc) · 11.7 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
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
#ifndef __FRACTION_H__
#define __FRACTION_H__
#pragma once
// #include <gperftools/profiler.h>
#include <cstdint>
#include <format>
#include <iostream>
#include <limits>
#include <type_traits>
#define MathUtils_SignBit(x) (((signed char *)&x)[sizeof(x) - 1] >> 7 | 1)
namespace caesar {
using namespace std;
template <typename T>
concept UnsignedType = std::is_unsigned_v<T>;
template <typename T>
concept UnsignedInt = UnsignedType<T> && std::is_same_v<T, unsigned int>;
template <typename T>
concept UnsignedLong = UnsignedType<T> && std::is_same_v<T, unsigned long>;
template <typename T>
concept UnsignedLongLong = UnsignedType<T> && std::is_same_v<T, unsigned long long>;
template <UnsignedType T>
class Fraction {
private:
int8_t sign;
T numer, denom;
double val;
public:
Fraction();
template<typename T1>
Fraction(const T1 &_numer, const T1 &_denom); // a/b
template <UnsignedType T1>
requires(sizeof(T1) <= sizeof(T))
Fraction(const Fraction<T1> &u);
Fraction(const T &_numer, const T &_denom, const int8_t &_sign); // op * a/b
auto ConvToFloat() const -> double;
auto get_numer() const -> T;
auto get_denom() const -> T;
auto get_sign() const -> int8_t;
void Simplify();
};
// https://www.luogu.com.cn/blog/maysoul/solution-p5435
template <UnsignedType T1, UnsignedType T2>
auto gcd(T1 a, T2 b) -> std::common_type_t<T1, T2> {
int az = __builtin_ctzll(a), bz = __builtin_ctzll(b);
int z = min(az, bz);
int64_t dif;
b >>= bz;
while (a) {
a >>= az;
dif = b - a;
az = __builtin_ctzll(dif);
if (a < b) b = a;
if (dif < 0)
a = -dif;
else
a = dif;
}
return b << z;
}
// https://www.luogu.com.cn/blog/maysoul/solution-p5435
template <UnsignedType T1, UnsignedType T2> requires UnsignedInt<std::common_type_t<T1, T2>>
auto gcd(T1 a, T2 b) -> std::common_type_t<T1, T2> {
int az = __builtin_ctz(a), bz = __builtin_ctz(b);
int z = min(az, bz);
int dif;
b >>= bz;
while (a) {
a >>= az;
dif = b - a;
az = __builtin_ctz(dif);
if (a < b) b = a;
if (dif < 0)
a = -dif;
else
a = dif;
}
return b << z;
}
template <UnsignedType T1, UnsignedType T2> requires UnsignedLong<std::common_type_t<T1, T2>>
auto gcd(T1 a, T2 b) -> std::common_type_t<T1, T2> {
int az = __builtin_ctzl(a), bz = __builtin_ctzl(b);
int z = min(az, bz);
long dif;
b >>= bz;
while (a) {
a >>= az;
dif = b - a;
az = __builtin_ctzl(dif);
if (a < b) b = a;
if (dif < 0)
a = -dif;
else
a = dif;
}
return b << z;
}
template <UnsignedType T1, UnsignedType T2> requires UnsignedLongLong<std::common_type_t<T1, T2>>
auto gcd(T1 a, T2 b) -> std::common_type_t<T1, T2> {
int az = __builtin_ctzll(a), bz = __builtin_ctzll(b);
int z = min(az, bz);
int64_t dif;
b >>= bz;
while (a) {
a >>= az;
dif = b - a;
az = __builtin_ctzll(dif);
if (a < b) b = a;
if (dif < 0)
a = -dif;
else
a = dif;
}
return b << z;
}
template <typename T1, typename T2>
requires UnsignedInt<std::common_type_t<T1, T2>>
inline auto check_add_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned int ans;
__builtin_uadd_overflow(_x, _y, &ans);
return ans;
}
template <typename T1, typename T2>
requires UnsignedLong<std::common_type_t<T1, T2>>
inline auto check_add_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned long ans;
__builtin_uaddl_overflow(_x, _y, &ans);
return ans;
}
template <typename T1, typename T2>
requires UnsignedLongLong<std::common_type_t<T1, T2>>
inline auto check_add_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned long long ans;
__builtin_uaddll_overflow(_x, _y, &ans);
return ans;
}
template <typename T1, typename T2>
requires UnsignedInt<std::common_type_t<T1, T2>>
inline auto check_mul_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned int ans;
__builtin_umul_overflow(_x, _y, &ans);
return ans;
}
template <typename T1, typename T2>
requires UnsignedLong<std::common_type_t<T1, T2>>
inline auto check_mul_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned long ans;
__builtin_umull_overflow(_x, _y, &ans);
return ans;
}
template <typename T1, typename T2>
requires UnsignedLongLong<std::common_type_t<T1, T2>>
inline auto check_mul_overflow(const T1 &_x, const T2 &_y) -> std::common_type_t<T1, T2> {
unsigned long long ans;
__builtin_umulll_overflow(_x, _y, &ans);
return ans;
}
template <UnsignedType T>
Fraction<T>::Fraction() {
this->sign = 1;
this->numer = 0;
this->denom = 1;
this->val = 0;
}
template <UnsignedType T>
template <typename T1>
Fraction<T>::Fraction(const T1 &_numer, const T1 &_denom) {
if (_denom == 0) {
cout << "divided by 0" << endl;
throw("input error");
}
this->sign = MathUtils_SignBit(numer) * MathUtils_SignBit(denom);
this->numer = _numer * MathUtils_SignBit(numer);
this->denom = _denom * MathUtils_SignBit(denom);
Simplify();
}
template <UnsignedType T>
Fraction<T>::Fraction(const T &_numer, const T &_denom, const int8_t &_sign) {
if ((_sign != 1 && _sign != -1) || _numer < 0 || _denom <= 0) {
cout << format("input error: op = {}, numer = {}, denom = {}", _sign, _numer, _denom) << endl;
throw("input error");
}
this->sign = _sign;
this->numer = _numer;
this->denom = _denom;
Simplify();
}
template <UnsignedType T>
template <UnsignedType T1>
requires(sizeof(T1) <= sizeof(T))
Fraction<T>::Fraction(const Fraction<T1> &u) {
this->numer = u.get_numer();
this->denom = u.get_denom();
this->sign = u.get_sign();
this->val = u.ConvToFloat();
}
template <UnsignedType T>
void Fraction<T>::Simplify() {
if (this->numer == 0) {
this->denom = 1;
this->sign = 1;
this->val = 0;
} else {
auto g = gcd(this->numer, this->denom);
this->numer = this->numer / g;
this->denom = this->denom / g;
this->val = (double)this->numer / this->denom * this->sign;
}
}
template <UnsignedType T>
auto Fraction<T>::get_numer() const -> T {
return this->numer;
}
template <UnsignedType T>
auto Fraction<T>::get_denom() const -> T {
return this->denom;
}
template <UnsignedType T>
auto Fraction<T>::get_sign() const -> int8_t {
return this->sign;
}
template <UnsignedType T>
inline auto Fraction<T>::ConvToFloat() const -> double {
return this->val;
}
template <UnsignedType T1, UnsignedType T2>
auto operator+(const Fraction<T1> &u, const Fraction<T2> &v) {
// ProfilerStart("cpp_demo_perf.prof");
if (u.get_sign() == v.get_sign()) {
auto d1 = gcd(u.get_denom(), v.get_denom());
if (d1 > 1) {
auto t = check_add_overflow(check_mul_overflow(u.get_numer(), (v.get_denom() / d1)),
check_mul_overflow(v.get_numer(), (u.get_denom() / d1)));
auto d2 = gcd(t, d1);
return Fraction<std::common_type_t<T1, T2>>(t / d2, check_mul_overflow((u.get_denom() / d1) , (v.get_denom() / d2)),
u.get_sign());
} else {
auto t = check_add_overflow(check_mul_overflow(u.get_numer(), v.get_denom()),
check_mul_overflow(v.get_numer(), u.get_denom()));
return Fraction<std::common_type_t<T1, T2>>(t, check_mul_overflow(u.get_denom() , v.get_denom()), u.get_sign());
}
} else {
auto d1 = gcd(u.get_denom(), v.get_denom());
if(d1 > 1) {
auto u1_numer = check_mul_overflow(u.get_numer(), (v.get_denom() / d1));
auto v1_numer = check_mul_overflow(v.get_numer(), (u.get_denom() / d1));
if (u1_numer > v1_numer) {
auto t = u1_numer - v1_numer;
auto d2 = gcd(t, d1);
return Fraction<std::common_type_t<T1, T2>>(t / d2, check_mul_overflow((u.get_denom() / d1) , (v.get_denom() / d2)),
u.get_sign());
} else {
auto t = v1_numer - u1_numer;
auto d2 = gcd(t, d1);
return Fraction<std::common_type_t<T1, T2>>(t / d2, check_mul_overflow((u.get_denom() / d1) , (v.get_denom() / d2)),
-u.get_sign());
}
} else {
auto u1_numer = check_mul_overflow(u.get_numer(), v.get_denom());
auto v1_numer = check_mul_overflow(v.get_numer(), u.get_denom());
if (u1_numer > v1_numer) {
auto t = u1_numer - v1_numer;
return Fraction<std::common_type_t<T1, T2>>(t, check_mul_overflow(u.get_denom() , v.get_denom()), u.get_sign());
} else {
auto t = v1_numer - u1_numer;
return Fraction<std::common_type_t<T1, T2>>(t, check_mul_overflow(u.get_denom() , v.get_denom()), -u.get_sign());
}
}
}
// ProfilerStop();
}
template <UnsignedType T>
auto operator-(const Fraction<T> &u) -> Fraction<T> {
return Fraction<T>(u.get_numer(), u.get_denom(), -u.get_sign());
}
template <UnsignedType T1, UnsignedType T2>
auto operator-(const Fraction<T1> &u, const Fraction<T2> &v) -> Fraction<std::common_type_t<T1, T2>> {
return u + (-v);
}
template <UnsignedType T1, UnsignedType T2>
auto operator*(const Fraction<T1> &u, const Fraction<T2> &v) -> Fraction<std::common_type_t<T1, T2>> {
auto d1 = gcd(u.get_numer(), v.get_denom());
auto d2 = gcd(u.get_denom(), v.get_numer());
auto u_numer = u.get_numer() / d1;
auto u_denom = u.get_denom() / d2;
auto v_numer = v.get_numer() / d2;
auto v_denom = v.get_denom() / d1;
return Fraction<typename std::common_type_t<T1, T2>>{check_mul_overflow(u_numer , v_numer) , check_mul_overflow(u_denom , v_denom) ,
u.get_sign() * v.get_sign()};
}
template <UnsignedType T1, UnsignedType T2>
auto operator/(const Fraction<T1> &u, const Fraction<T2> &v) -> Fraction<std::common_type_t<T1, T2>> {
if (v.get_numer() == 0) {
cout << format("overflow at operator '/': u = {}{}/{}, v = {}{}/{}", u.get_sign() == -1 ? '-' : ' ',
u.get_numer(), u.get_denom(), v.get_sign() == -1 ? '-' : ' ', v.get_numer(),
v.get_denom())
<< endl;
}
auto d1 = gcd(u.get_numer(), v.get_numer());
auto d2 = gcd(u.get_denom(), v.get_denom());
auto u_numer = u.get_numer() / d1;
auto u_denom = u.get_denom() / d2;
auto v_numer = v.get_numer() / d1;
auto v_denom = v.get_denom() / d2;
return Fraction<typename std::common_type_t<T1, T2>>{check_mul_overflow(u_numer , v_denom) , check_mul_overflow(u_denom , v_numer),
u.get_sign() * v.get_sign()};
}
template <UnsignedType T1, UnsignedType T2>
auto operator<(const Fraction<T1> &u, const Fraction<T2> &v) -> bool {
return u.ConvToFloat() - v.ConvToFloat() < 0;
}
template <UnsignedType T1, UnsignedType T2>
auto operator>(const Fraction<T1> &u, const Fraction<T2> &v) -> bool {
return u.ConvToFloat() - v.ConvToFloat() > 0;
}
template <UnsignedType T1, UnsignedType T2>
auto operator==(const Fraction<T1> &u, const Fraction<T2> &v) -> bool {
if (u.get_sign() == v.get_sign() && u.get_numer() == v.get_numer() && u.get_denom() == v.get_denom())
return true;
return false;
}
} // namespace caesar
#endif