-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrational.h
More file actions
145 lines (118 loc) · 3.67 KB
/
rational.h
File metadata and controls
145 lines (118 loc) · 3.67 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
//
// Created by shama on 15.04.2023.
//
#ifndef VKR_RATIONAL_H
#define VKR_RATIONAL_H
#include "save_integral.h"
template<typename Integral>
Integral GCD(Integral a, Integral b) {
while (a != 0) {
Integral t = a;
a = b % a;
b = t;
}
return b;
}
template<typename Integral>
Integral LCM(Integral a, Integral b) {
return a / GCD(a, b) * b;
}
class Rational {
SafeIntegral numerator = SafeIntegral(0);
SafeIntegral denominator = SafeIntegral(1);
static SafeIntegral Reduce(SafeIntegral& a, SafeIntegral& b) {
SafeIntegral d = GCD(a, b);
a /= d;
b /= d;
return d;
}
public:
Rational() = default;
explicit Rational(Integral value) : numerator(value) {}
Rational(Integral numerator, Integral denominator) : numerator(numerator), denominator(denominator) {}
explicit Rational(SafeIntegral numerator, SafeIntegral denominator = SafeIntegral(1)) : numerator(numerator), denominator(denominator) {}
Rational& operator++() {
numerator += denominator;
return *this;
}
Rational operator+=(Rational other) {
auto d = Reduce(denominator, other.denominator);
numerator *= other.denominator;
other.numerator *= denominator;
numerator += other.numerator;
Reduce(numerator, d);
denominator *= d;
denominator *= other.denominator;
return *this;
}
Rational operator+(Rational other) const {
Rational res = *this;
return res += other;
}
Rational operator-=(Rational other) {
auto d = Reduce(denominator, other.denominator);
numerator *= other.denominator;
other.numerator *= denominator;
numerator -= other.numerator;
Reduce(numerator, d);
denominator *= d;
denominator *= other.denominator;
return *this;
}
Rational operator-(Rational other) const {
Rational res = *this;
return res -= other;
}
Rational operator/=(Rational other) {
Reduce(numerator, other.numerator);
Reduce(denominator, other.denominator);
numerator *= other.denominator;
denominator *= other.numerator;
return *this;
}
Rational operator/(Rational other) const {
Rational res = *this;
return res /= other;
}
Rational operator*=(Rational other) {
Reduce(numerator, other.denominator);
Reduce(denominator, other.numerator);
numerator *= other.numerator;
denominator *= other.denominator;
return *this;
}
Rational operator*(Rational other) const {
Rational res = *this;
return res *= other;
}
COMPARABLE_AS(numerator * other.denominator, denominator * other.numerator)
static Rational Ceil(Rational r) {
return Rational(r.numerator / r.denominator + SafeIntegral(r.numerator % r.denominator != 0 ? 1 : 0));
}
static Rational Floor(Rational r) {
return Rational(r.numerator / r.denominator);
}
static bool IsInteger(Rational rational) {
return rational.denominator == 1;
}
explicit operator SafeIntegral() const {
return numerator / denominator;
}
[[nodiscard]] SafeIntegral GetNumerator() const {
return numerator;
}
[[nodiscard]] SafeIntegral GetDenominator() const {
return denominator;
}
[[nodiscard]] static SafeIntegral GetNumerator(Rational r) {
return r.numerator;
}
[[nodiscard]] static SafeIntegral GetDenominator(Rational r) {
return r.denominator;
}
};
Rational operator/(Integral x, Rational other) {
auto res = Rational(x);
return res /= other;
}
#endif //VKR_RATIONAL_H