-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfraction.cpp
More file actions
453 lines (376 loc) · 14.2 KB
/
fraction.cpp
File metadata and controls
453 lines (376 loc) · 14.2 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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
/*
* fraction.cpp
*
* Copyright (C) 2023-2024 Max Qian <lightapt.com>
*/
/*************************************************
Date: 2024-3-28
Description: Implementation of Fraction class
**************************************************/
#include "fraction.hpp"
#include <cmath>
#include <sstream>
// Check if SSE4.1 or higher is supported
#if defined(__SSE4_1__) || defined(__AVX__) || defined(__AVX2__)
#include <immintrin.h>
#define ATOM_FRACTION_USE_SIMD
#endif
namespace atom::algorithm {
/* ------------------------ Arithmetic Operators ------------------------ */
auto Fraction::operator+=(const Fraction& other) -> Fraction& {
try {
if (other.numerator == 0)
return *this;
if (numerator == 0) {
numerator = other.numerator;
denominator = other.denominator;
return *this;
}
long long commonDenominator =
static_cast<long long>(denominator) * other.denominator;
long long newNumerator =
static_cast<long long>(numerator) * other.denominator +
static_cast<long long>(other.numerator) * denominator;
// Check for overflow
if (newNumerator > std::numeric_limits<int>::max() ||
newNumerator < std::numeric_limits<int>::min() ||
commonDenominator > std::numeric_limits<int>::max() ||
commonDenominator < std::numeric_limits<int>::min()) {
throw FractionException("Integer overflow during addition.");
}
numerator = static_cast<int>(newNumerator);
denominator = static_cast<int>(commonDenominator);
reduce();
} catch (const std::exception& e) {
throw FractionException(std::string("Error in operator+=: ") +
e.what());
}
return *this;
}
auto Fraction::operator-=(const Fraction& other) -> Fraction& {
try {
// Fast path: if the subtrahend is 0, do nothing
if (other.numerator == 0)
return *this;
// Use safe long long calculations to prevent overflow
long long commonDenominator =
static_cast<long long>(denominator) * other.denominator;
long long newNumerator =
static_cast<long long>(numerator) * other.denominator -
static_cast<long long>(other.numerator) * denominator;
// Check for overflow
if (newNumerator > std::numeric_limits<int>::max() ||
newNumerator < std::numeric_limits<int>::min() ||
commonDenominator > std::numeric_limits<int>::max() ||
commonDenominator < std::numeric_limits<int>::min()) {
throw FractionException("Integer overflow during subtraction.");
}
numerator = static_cast<int>(newNumerator);
denominator = static_cast<int>(commonDenominator);
reduce();
} catch (const std::exception& e) {
throw FractionException(std::string("Error in operator-=: ") +
e.what());
}
return *this;
}
auto Fraction::operator*=(const Fraction& other) -> Fraction& {
try {
// Fast path: if the multiplier is 0, the result is 0
if (other.numerator == 0 || numerator == 0) {
numerator = 0;
denominator = 1;
return *this;
}
// Pre-calculate gcd to maximize reduction effect
int gcd1 = gcd(numerator, other.denominator);
int gcd2 = gcd(denominator, other.numerator);
// Pre-reduction can reduce overflow risk
long long n = (static_cast<long long>(numerator) / gcd1) *
(static_cast<long long>(other.numerator) / gcd2);
long long d = (static_cast<long long>(denominator) / gcd2) *
(static_cast<long long>(other.denominator) / gcd1);
// Check for overflow
if (n > std::numeric_limits<int>::max() ||
n < std::numeric_limits<int>::min() ||
d > std::numeric_limits<int>::max() ||
d < std::numeric_limits<int>::min()) {
throw FractionException("Integer overflow during multiplication.");
}
numerator = static_cast<int>(n);
denominator = static_cast<int>(d);
// Reduce again to ensure simplest form
reduce();
} catch (const std::exception& e) {
throw FractionException(std::string("Error in operator*=: ") +
e.what());
}
return *this;
}
auto Fraction::operator/=(const Fraction& other) -> Fraction& {
try {
if (other.numerator == 0) {
throw FractionException("Division by zero.");
}
// Pre-calculate gcd to maximize reduction effect
int gcd1 = gcd(numerator, other.numerator);
int gcd2 = gcd(denominator, other.denominator);
// Pre-reduction can reduce overflow risk
long long n = (static_cast<long long>(numerator) / gcd1) *
(static_cast<long long>(other.denominator) / gcd2);
long long d = (static_cast<long long>(denominator) / gcd2) *
(static_cast<long long>(other.numerator) / gcd1);
// Ensure denominator is not zero
if (d == 0) {
throw FractionException(
"Denominator cannot be zero after division.");
}
// Check for overflow
if (n > std::numeric_limits<int>::max() ||
n < std::numeric_limits<int>::min() ||
d > std::numeric_limits<int>::max() ||
d < std::numeric_limits<int>::min()) {
throw FractionException("Integer overflow during division.");
}
numerator = static_cast<int>(n);
denominator = static_cast<int>(d);
// Ensure denominator is positive
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
// Reduce again to ensure simplest form
reduce();
} catch (const std::exception& e) {
throw FractionException(std::string("Error in operator/=: ") +
e.what());
}
return *this;
}
/* ------------------------ Arithmetic Operators (Non-Member)
* ------------------------ */
auto Fraction::operator+(const Fraction& other) const -> Fraction {
Fraction result(*this);
result += other;
return result;
}
auto Fraction::operator-(const Fraction& other) const -> Fraction {
Fraction result(*this);
result -= other;
return result;
}
auto Fraction::operator*(const Fraction& other) const -> Fraction {
Fraction result(*this);
result *= other;
return result;
}
auto Fraction::operator/(const Fraction& other) const -> Fraction {
Fraction result(*this);
result /= other;
return result;
}
/* ------------------------ Comparison Operators ------------------------ */
#if __cplusplus >= 202002L
auto Fraction::operator<=>(const Fraction& other) const
-> std::strong_ordering {
// Use cross-multiplication to compare fractions, avoiding overflow
long long lhs = static_cast<long long>(numerator) * other.denominator;
long long rhs = static_cast<long long>(other.numerator) * denominator;
if (lhs < rhs) {
return std::strong_ordering::less;
}
if (lhs > rhs) {
return std::strong_ordering::greater;
}
return std::strong_ordering::equal;
}
#else
bool Fraction::operator<(const Fraction& other) const noexcept {
// Use cross-multiplication for comparison, avoiding division
return static_cast<long long>(numerator) * other.denominator <
static_cast<long long>(other.numerator) * denominator;
}
bool Fraction::operator<=(const Fraction& other) const noexcept {
return static_cast<long long>(numerator) * other.denominator <=
static_cast<long long>(other.numerator) * denominator;
}
bool Fraction::operator>(const Fraction& other) const noexcept {
return static_cast<long long>(numerator) * other.denominator >
static_cast<long long>(other.numerator) * denominator;
}
bool Fraction::operator>=(const Fraction& other) const noexcept {
return static_cast<long long>(numerator) * other.denominator >=
static_cast<long long>(other.numerator) * denominator;
}
#endif
bool Fraction::operator==(const Fraction& other) const noexcept {
#if __cplusplus >= 202002L
return (*this <=> other) == std::strong_ordering::equal;
#else
// Since we always reduce fractions to their simplest form,
// we can directly compare numerators and denominators.
return (numerator == other.numerator) && (denominator == other.denominator);
#endif
}
/* ------------------------ Utility Methods ------------------------ */
auto Fraction::toString() const -> std::string {
std::ostringstream oss;
oss << numerator << '/' << denominator;
return oss.str();
}
auto Fraction::invert() -> Fraction& {
if (numerator == 0) {
throw FractionException(
"Cannot invert a fraction with numerator zero.");
}
std::swap(numerator, denominator);
if (denominator < 0) {
numerator = -numerator;
denominator = -denominator;
}
return *this;
}
std::optional<Fraction> Fraction::pow(int exponent) const noexcept {
try {
// Handle special cases
if (exponent == 0) {
// Any number to the power of 0 is 1
return Fraction(1, 1);
}
if (exponent == 1) {
// Power of 1 is itself
return *this;
}
if (numerator == 0) {
// 0 to any positive power is 0, negative power is invalid
return exponent > 0 ? std::optional<Fraction>(Fraction(0, 1))
: std::nullopt;
}
// Handle negative exponent
bool isNegativeExponent = exponent < 0;
exponent = std::abs(exponent);
// Calculate power
long long resultNumerator = 1;
long long resultDenominator = 1;
long long n = numerator;
long long d = denominator;
// Use exponentiation by squaring (or simple iteration for now)
for (int i = 0; i < exponent; i++) {
resultNumerator *= n;
resultDenominator *= d;
// Check for overflow
if (resultNumerator > std::numeric_limits<int>::max() ||
resultNumerator < std::numeric_limits<int>::min() ||
resultDenominator > std::numeric_limits<int>::max() ||
resultDenominator < std::numeric_limits<int>::min()) {
return std::nullopt; // Overflow, return empty
}
}
// If negative exponent, swap numerator and denominator
if (isNegativeExponent) {
if (resultNumerator == 0) {
return std::nullopt; // Cannot take negative power, denominator
// would be 0
}
std::swap(resultNumerator, resultDenominator);
}
// If denominator is negative, adjust signs
if (resultDenominator < 0) {
resultNumerator = -resultNumerator;
resultDenominator = -resultDenominator;
}
Fraction result(static_cast<int>(resultNumerator),
static_cast<int>(resultDenominator));
return result;
} catch (...) {
return std::nullopt;
}
}
std::optional<Fraction> Fraction::fromString(std::string_view str) noexcept {
try {
std::size_t pos = str.find('/');
if (pos == std::string_view::npos) {
// Try to parse the whole string as an integer
int value = std::stoi(std::string(str));
return Fraction(value, 1);
} else {
// Parse numerator and denominator
std::string numeratorStr(str.substr(0, pos));
std::string denominatorStr(str.substr(pos + 1));
int n = std::stoi(numeratorStr);
int d = std::stoi(denominatorStr);
if (d == 0) {
return std::nullopt; // Denominator cannot be zero
}
return Fraction(n, d);
}
} catch (...) {
return std::nullopt; // Parsing failed or other exception
}
}
/* ------------------------ Friend Functions ------------------------ */
auto operator<<(std::ostream& os, const Fraction& f) -> std::ostream& {
os << f.toString();
return os;
}
auto operator>>(std::istream& is, Fraction& f) -> std::istream& {
int n = 0, d = 1;
char sep = '\0';
// First, try to read the numerator
if (!(is >> n)) {
is.setstate(std::ios::failbit);
throw FractionException("Failed to read numerator.");
}
// Check if the next character is the separator '/'
if (is.peek() == '/') {
is.get(sep); // Read the separator
// Try to read the denominator
if (!(is >> d)) {
is.setstate(std::ios::failbit);
throw FractionException("Failed to read denominator after '/'.");
}
if (d == 0) {
is.setstate(std::ios::failbit);
throw FractionException("Denominator cannot be zero.");
}
}
// Set the fraction value and reduce
f.numerator = n;
f.denominator = d;
f.reduce();
return is;
}
/* ------------------------ Global Utility Functions ------------------------ */
auto makeFraction(double value, int max_denominator) -> Fraction {
if (std::isnan(value) || std::isinf(value)) {
throw FractionException("Cannot create Fraction from NaN or Infinity.");
}
// Handle zero
if (value == 0.0) {
return Fraction(0, 1);
}
// Handle sign
int sign = (value < 0) ? -1 : 1;
value = std::abs(value);
// Use continued fraction algorithm for more accurate approximation
double epsilon = 1.0 / max_denominator;
int a = static_cast<int>(std::floor(value));
double f_val = value - a; // Renamed to avoid conflict with ostream f
int h1 = 1, h2 = a;
int k1 = 0, k2 = 1;
while (f_val > epsilon && k2 < max_denominator) {
double r = 1.0 / f_val;
a = static_cast<int>(std::floor(r));
f_val = r - a;
int h = a * h2 + h1;
int k = a * k2 + k1;
if (k > max_denominator)
break;
h1 = h2;
h2 = h;
k1 = k2;
k2 = k;
}
return Fraction(sign * h2, k2);
}
} // namespace atom::algorithm