-
Notifications
You must be signed in to change notification settings - Fork 0
/
Fraction.hpp
235 lines (188 loc) · 6.01 KB
/
Fraction.hpp
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
#pragma once
#include <type_traits>
#include "Math.hpp"
template<typename T>
concept isFraction = requires (T fraction) {
fraction.numerator();
fraction.denominator();
};
template<typename T>
requires(std::is_integral_v<T> || std::is_base_of_v<MathType, T>)
class Fraction : public MathType {
T numerator_{};
T denominator_{ 1 };
constexpr void reduce() {
if constexpr (!isFraction<T>) {
while (true) {
const std::optional<T> divisor{ commonDivisor<T>(numerator_, denominator_) };
if (!divisor.has_value()) {
break;
}
numerator_ /= divisor.value();
denominator_ /= divisor.value();
}
}
else {
const auto division{ numerator_ / denominator_ };
numerator_ = division.numerator();
denominator_ = division.denominator();
}
}
public:
/*template<typename U>
requires(std::is_integral_v<U>)
constexpr static auto convertToFractionOfIntegers() noexcept {
}*/
constexpr explicit Fraction(T numerator, T denominator) noexcept {
if constexpr (isFraction<T>) {
*this = numerator / denominator;
}
else {
numerator_ = numerator;
denominator_ = denominator;
reduce();
}
}
constexpr Fraction(T result = 0) noexcept {
numerator_ = result;
}
constexpr Fraction(const Fraction<T>& other) noexcept {
numerator_ = other.numerator_;
denominator_ = other.denominator_;
}
constexpr T result() const noexcept(noexcept(T() / T())) {
return numerator_ / denominator_;
}
constexpr T numerator() const noexcept {
return numerator_;
}
constexpr T denominator() const noexcept {
return denominator_;
}
constexpr auto inverse() const noexcept {
return Fraction<T>(denominator_, numerator_);
}
constexpr auto operator+(const Fraction<T>& other) const noexcept {
if (denominator_ == other.denominator_) {
return Fraction<T>(numerator_ + other.numerator_, denominator_);
}
const T commonDenominator{ denominator_ * other.denominator_ };
const T correspondingNumerator{ numerator_ * other.denominator_ + other.numerator_ * denominator_ };
return Fraction<T>(correspondingNumerator, commonDenominator);
}
constexpr auto operator+(T other) const noexcept {
return *this + Fraction<T>(other);
}
constexpr auto operator-() const noexcept {
return Fraction<T>(-numerator_, denominator_);
}
constexpr auto operator-(const Fraction<T>& other) const noexcept {
return *this + -other;
}
constexpr auto operator-(T other) const noexcept {
return *this - Fraction<T>(other);
}
constexpr auto operator*(const Fraction<T>& other) const noexcept {
return Fraction<T>(numerator_ * other.numerator_, denominator_ * other.denominator_);
}
constexpr auto operator*(T other) const noexcept {
return Fraction<T>(numerator_ * other, denominator_);
}
constexpr auto operator/(const Fraction<T>& other) const noexcept {
return *this * other.inverse();
}
constexpr auto operator/(T other) const noexcept {
return Fraction<T>(numerator_, denominator_ * other);
}
constexpr auto operator^(unsigned long long pow) const noexcept {
return Fraction<T>(power(numerator_, pow), power(denominator_, pow));
}
constexpr auto& operator+=(const Fraction<T>& other) noexcept {
return (*this = *this + other);
}
constexpr auto& operator+=(T other) noexcept {
return (*this = *this + other);
}
constexpr auto& operator++() noexcept {
return (*this += 1);
}
constexpr auto& operator++(int) noexcept {
auto& old{ *this };
++(*this);
return *this;
}
constexpr auto& operator-=(const Fraction<T>& other) noexcept {
return (*this = *this - other);
}
constexpr auto& operator-=(T other) noexcept {
return (*this = *this - other);
}
constexpr auto& operator--() noexcept {
return (*this -= 1);
}
constexpr auto& operator--(int) noexcept {
auto& old{ *this };
--(*this);
return *this;
}
constexpr auto& operator*=(const Fraction<T>& other) noexcept {
return (*this = *this * other);
}
constexpr auto& operator*=(T other) noexcept {
return (*this = *this * other);
}
constexpr auto& operator/=(const Fraction<T>& other) noexcept {
return (*this = *this / other);
}
constexpr auto& operator/=(T other) noexcept {
return (*this = *this / other);
}
constexpr auto& operator^=(unsigned long long pow) noexcept {
return (*this = *this ^ pow);
}
template<typename U>
requires (std::is_arithmetic_v<U> && !std::is_same_v<T, U> && std::is_convertible_v<T, U>)
constexpr operator U() const noexcept {
return Fraction<U>(static_cast<U>(numerator_), static_cast<U>(denominator_)).result();
}
constexpr bool operator==(const Fraction<T>& other) const noexcept {
return numerator_ == other.numerator() && denominator_ == other.denominator();
}
constexpr bool operator==(T result) const noexcept {
return this->result() == result;
}
constexpr bool operator!=(const Fraction<T>& other) const noexcept {
return !(*this == other);
}
constexpr bool operator!=(T result) const noexcept {
return !(*this == result);
}
};
template<typename T>
constexpr auto operator+(T first, const Fraction<T>& other) noexcept {
return other + first;
}
template<typename T>
constexpr auto operator-(T first, const Fraction<T>& other) noexcept {
return Fraction<T>(first) - other;
}
template<typename T>
constexpr auto operator*(T first, const Fraction<T>& other) noexcept {
return other * first;
}
template<typename T>
constexpr auto operator/(T first, const Fraction<T>& other) noexcept {
return Fraction<T>(first) / other;
}
template<typename T>
constexpr auto operator/(const Fraction<T>& divided, T divisor) noexcept {
return Fraction<T>(divided.numerator(), divided.denominator() * divisor);
}
template<typename T>
constexpr bool operator==(T result, const Fraction<T>& other) noexcept {
return result == other.result();
}
template<typename T>
constexpr bool operator!=(T result, const Fraction<T>& other) noexcept {
return !(result == other);
}