-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBigReal.h
390 lines (334 loc) · 17.4 KB
/
BigReal.h
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
#pragma once
/*
* /File: BigReal.h /
* /Brief: BigInt and BigReal
* /Creator: Leonid Artemev | Leartiz /
* /Date: 2020 /
*/
#include <vector>
#include <string>
namespace lez
{
class BigInt; // forward declaration
// start_amount_digits_after_comma() edit for all class
// division by zero is zero!
class BigReal
{
public:
BigReal();
BigReal(const BigReal& val_);
BigReal(BigReal&& val_);
explicit BigReal(std::int32_t val_); // or implicit
explicit BigReal(std::size_t val_);
explicit BigReal(std::int64_t val_);
explicit BigReal(std::uint64_t val_);
explicit BigReal(const BigInt& val_);
explicit BigReal(float val_);
explicit BigReal(double val_);
BigReal& operator=(std::int32_t val_);
BigReal& operator=(std::size_t val_);
BigReal& operator=(std::int64_t val_);
BigReal& operator=(std::uint64_t val_);
BigReal& operator=(const BigInt& val_);
BigReal& operator=(float val_);
BigReal& operator=(double val_);
BigReal& operator=(BigReal val_);
std::int32_t to_int32() const;
std::size_t to_size() const;
std::int64_t to_int64() const;
std::uint64_t to_uint64() const;
BigInt to_big_int() const;
float to_float() const;
double to_double() const;
std::size_t amount_places() const; // only numbers
std::size_t amount_places_whole_part() const;
std::size_t amount_places_fractional_part() const;
bool is_neg() const;
bool is_zero() const;
void reverse_neg();
void set_neg();
void reset_neg();
void reset();
explicit operator bool() const;
explicit operator std::int32_t() const;
explicit operator std::size_t() const;
explicit operator std::int64_t() const;
explicit operator std::uint64_t() const;
explicit operator BigInt() const;
explicit operator float() const;
explicit operator double() const;
const BigReal operator-() const;
const BigReal operator+() const;
BigReal& operator--();
BigReal& operator++();
const BigReal operator--(int);
const BigReal operator++(int);
const BigReal operator+(std::int32_t r_val_) const;
const BigReal operator+(std::size_t r_val_) const;
const BigReal operator+(std::int64_t r_val_) const;
const BigReal operator+(std::uint64_t r_val_) const;
const BigReal operator+(const BigInt& r_val_) const;
const BigReal operator+(float r_val_) const;
const BigReal operator+(double r_val_) const;
const BigReal operator+(const BigReal& r_val_) const;
const BigReal operator-(std::int32_t r_val_) const;
const BigReal operator-(std::size_t r_val_) const;
const BigReal operator-(std::int64_t r_val_) const;
const BigReal operator-(std::uint64_t r_val_) const;
const BigReal operator-(const BigInt& r_val_) const;
const BigReal operator-(float r_val_) const;
const BigReal operator-(double r_val_) const;
const BigReal operator-(const BigReal& r_val_) const;
BigReal& operator+=(std::int32_t r_val_);
BigReal& operator+=(std::size_t r_val_);
BigReal& operator+=(std::int64_t r_val_);
BigReal& operator+=(std::uint64_t r_val_);
BigReal& operator+=(const BigInt& r_val_);
BigReal& operator+=(float r_val_);
BigReal& operator+=(double r_val_);
BigReal& operator+=(const BigReal& r_val_);
BigReal& operator-=(std::int32_t r_val_);
BigReal& operator-=(std::size_t r_val_);
BigReal& operator-=(std::int64_t r_val_);
BigReal& operator-=(std::uint64_t r_val_);
BigReal& operator-=(const BigInt& r_val_);
BigReal& operator-=(float r_val_);
BigReal& operator-=(double r_val_);
BigReal& operator-=(const BigReal& r_val_);
friend const BigReal operator+(std::int32_t l_val_, const BigReal& r_val_);
friend const BigReal operator+(std::size_t l_val_, const BigReal& r_val_);
friend const BigReal operator+(std::int64_t l_val_, const BigReal& r_val_);
friend const BigReal operator+(std::uint64_t l_val_, const BigReal& r_val_);
friend const BigReal operator+(const BigInt& l_val_, const BigReal& r_val_);
friend const BigReal operator+(float l_val_, const BigReal& r_val_);
friend const BigReal operator+(double l_val_, const BigReal& r_val_);
friend const BigReal operator-(std::int32_t l_val_, const BigReal& r_val_);
friend const BigReal operator-(std::size_t l_val_, const BigReal& r_val_);
friend const BigReal operator-(std::int64_t l_val_, const BigReal& r_val_);
friend const BigReal operator-(std::uint64_t l_val_, const BigReal& r_val_);
friend const BigReal operator-(const BigInt& l_val_, const BigReal& r_val_);
friend const BigReal operator-(float l_val_, const BigReal& r_val_);
friend const BigReal operator-(double l_val_, const BigReal& r_val_);
const BigReal operator*(std::int32_t r_val_) const;
const BigReal operator*(std::size_t r_val_) const;
const BigReal operator*(std::int64_t r_val_) const;
const BigReal operator*(std::uint64_t r_val_) const;
const BigReal operator*(const BigInt& r_val_) const;
const BigReal operator*(float r_val_) const;
const BigReal operator*(double r_val_) const;
const BigReal operator*(const BigReal& r_val_) const;
const BigReal operator/(std::int32_t r_val_) const;
const BigReal operator/(std::size_t r_val_) const;
const BigReal operator/(std::int64_t r_val_) const;
const BigReal operator/(std::uint64_t r_val_) const;
const BigReal operator/(const BigInt& r_val_) const;
const BigReal operator/(float r_val_) const;
const BigReal operator/(double r_val_) const;
const BigReal operator/(const BigReal& r_val_) const;
const BigReal operator%(std::int32_t r_val_) const;
const BigReal operator%(std::size_t r_val_) const;
const BigReal operator%(std::int64_t r_val_) const;
const BigReal operator%(std::uint64_t r_val_) const;
const BigReal operator%(const BigInt& r_val_) const;
const BigReal operator%(float r_val_) const;
const BigReal operator%(double r_val_) const;
const BigReal operator%(const BigReal& r_val_) const;
BigReal& operator*=(std::int32_t r_val_);
BigReal& operator*=(std::size_t r_val_);
BigReal& operator*=(std::int64_t r_val_);
BigReal& operator*=(std::uint64_t r_val_);
BigReal& operator*=(const BigInt& r_val_);
BigReal& operator*=(float r_val_);
BigReal& operator*=(double r_val_);
BigReal& operator*=(const BigReal& r_val_);
BigReal& operator/=(std::int32_t r_val_);
BigReal& operator/=(std::size_t r_val_);
BigReal& operator/=(std::int64_t r_val_);
BigReal& operator/=(std::uint64_t r_val_);
BigReal& operator/=(const BigInt& r_val_);
BigReal& operator/=(float r_val_);
BigReal& operator/=(double r_val_);
BigReal& operator/=(const BigReal& r_val_);
BigReal& operator%=(std::int32_t r_val_);
BigReal& operator%=(std::size_t r_val_);
BigReal& operator%=(std::int64_t r_val_);
BigReal& operator%=(std::uint64_t r_val_);
BigReal& operator%=(const BigInt& r_val_);
BigReal& operator%=(float r_val_);
BigReal& operator%=(double r_val_);
BigReal& operator%=(const BigReal& r_val_);
friend const BigReal operator*(std::int32_t l_val_, const BigReal& r_val_);
friend const BigReal operator*(std::size_t l_val_, const BigReal& r_val_);
friend const BigReal operator*(std::int64_t l_val_, const BigReal& r_val_);
friend const BigReal operator*(std::uint64_t l_val_, const BigReal& r_val_);
friend const BigReal operator*(const BigInt& l_val_, const BigReal& r_val_);
friend const BigReal operator*(float l_val_, const BigReal& r_val_);
friend const BigReal operator*(double l_val_, const BigReal& r_val_);
friend const BigReal operator/(std::int32_t l_val_, const BigReal& r_val_);
friend const BigReal operator/(std::size_t l_val_, const BigReal& r_val_);
friend const BigReal operator/(std::int64_t l_val_, const BigReal& r_val_);
friend const BigReal operator/(std::uint64_t l_val_, const BigReal& r_val_);
friend const BigReal operator/(const BigInt& l_val_, const BigReal& r_val_);
friend const BigReal operator/(float l_val_, const BigReal& r_val_);
friend const BigReal operator/(double l_val_, const BigReal& r_val_);
friend const BigReal operator%(std::int32_t l_val_, const BigReal& r_val_);
friend const BigReal operator%(std::size_t l_val_, const BigReal& r_val_);
friend const BigReal operator%(std::int64_t l_val_, const BigReal& r_val_);
friend const BigReal operator%(std::uint64_t l_val_, const BigReal& r_val_);
friend const BigReal operator%(const BigInt& l_val_, const BigReal& r_val_);
friend const BigReal operator%(float l_val_, const BigReal& r_val_);
friend const BigReal operator%(double l_val_, const BigReal& r_val_);
bool operator>(std::int32_t r_val_) const;
bool operator>(std::size_t r_val_) const;
bool operator>(std::int64_t r_val_) const;
bool operator>(std::uint64_t r_val_) const;
bool operator>(const BigInt& r_val_) const;
bool operator>(float r_val_) const;
bool operator>(double r_val_) const;
bool operator>(const BigReal& r_val_) const;
bool operator<(std::int32_t r_val_) const;
bool operator<(std::size_t r_val_) const;
bool operator<(std::int64_t r_val_) const;
bool operator<(std::uint64_t r_val_) const;
bool operator<(const BigInt& r_val_) const;
bool operator<(float r_val_) const;
bool operator<(double r_val_) const;
bool operator<(const BigReal& r_val_) const;
bool operator>=(std::int32_t r_val_) const;
bool operator>=(std::size_t r_val_) const;
bool operator>=(std::int64_t r_val_) const;
bool operator>=(std::uint64_t r_val_) const;
bool operator>=(const BigInt& r_val_) const;
bool operator>=(float r_val_) const;
bool operator>=(double r_val_) const;
bool operator>=(const BigReal& r_val_) const;
bool operator<=(std::int32_t r_val_) const;
bool operator<=(std::size_t r_val_) const;
bool operator<=(std::int64_t r_val_) const;
bool operator<=(std::uint64_t r_val_) const;
bool operator<=(const BigInt& r_val_) const;
bool operator<=(float r_val_) const;
bool operator<=(double r_val_) const;
bool operator<=(const BigReal& r_val_) const;
bool operator==(std::int32_t r_val_) const;
bool operator==(std::size_t r_val_) const;
bool operator==(std::int64_t r_val_) const;
bool operator==(std::uint64_t r_val_) const;
bool operator==(const BigInt& r_val_) const;
bool operator==(float r_val_) const;
bool operator==(double r_val_) const;
bool operator==(const BigReal& r_val_) const;
bool operator!=(std::int32_t r_val_) const;
bool operator!=(std::size_t r_val_) const;
bool operator!=(std::int64_t r_val_) const;
bool operator!=(std::uint64_t r_val_) const;
bool operator!=(const BigInt& r_val_) const;
bool operator!=(float r_val_) const;
bool operator!=(double r_val_) const;
bool operator!=(const BigReal& r_val_) const;
friend bool operator>(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator>(std::size_t l_val_, const BigReal& r_val_);
friend bool operator>(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator>(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator>(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator>(float l_val_, const BigReal& r_val_);
friend bool operator>(double l_val_, const BigReal& r_val_);
friend bool operator<(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator<(std::size_t l_val_, const BigReal& r_val_);
friend bool operator<(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator<(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator<(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator<(float l_val_, const BigReal& r_val_);
friend bool operator<(double l_val_, const BigReal& r_val_);
friend bool operator>=(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator>=(std::size_t l_val_, const BigReal& r_val_);
friend bool operator>=(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator>=(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator>=(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator>=(float l_val_, const BigReal& r_val_);
friend bool operator>=(double l_val_, const BigReal& r_val_);
friend bool operator<=(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator<=(std::size_t l_val_, const BigReal& r_val_);
friend bool operator<=(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator<=(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator<=(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator<=(float l_val_, const BigReal& r_val_);
friend bool operator<=(double l_val_, const BigReal& r_val_);
friend bool operator==(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator==(std::size_t l_val_, const BigReal& r_val_);
friend bool operator==(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator==(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator==(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator==(float l_val_, const BigReal& r_val_);
friend bool operator==(double l_val_, const BigReal& r_val_);
friend bool operator!=(std::int32_t l_val_, const BigReal& r_val_);
friend bool operator!=(std::size_t l_val_, const BigReal& r_val_);
friend bool operator!=(std::int64_t l_val_, const BigReal& r_val_);
friend bool operator!=(std::uint64_t l_val_, const BigReal& r_val_);
friend bool operator!=(const BigInt& l_val_, const BigReal& r_val_);
friend bool operator!=(float l_val_, const BigReal& r_val_);
friend bool operator!=(double l_val_, const BigReal& r_val_);
std::size_t get_amount_digits_after_comma() const;
void set_amount_digits_after_comma(std::size_t am_dig_after_com_);
std::istream& operator>>(std::istream& stream_);
std::ostream& operator<<(std::ostream& stream_) const;
std::string to_string() const;
std::string to_string_unsigned() const;
std::string to_string_whole_part() const;
std::string to_string_unsigned_whole_part() const;
std::string to_string_fractional_part() const;
friend std::istream& operator>>(std::istream& stream_, BigReal& bi_);
friend std::ostream& operator<<(std::ostream& stream_, const BigReal& bi_);
static BigReal to_big_real(std::int32_t val_);
static BigReal to_big_real(std::size_t val_);
static BigReal to_big_real(std::int64_t val_);
static BigReal to_big_real(std::uint64_t val_);
static BigReal to_big_real(const BigInt& val_);
static BigReal to_big_real(float val_);
static BigReal to_big_real(double val_);
static BigReal to_big_real(const std::string& str_);
static void to_big_real(BigReal &br_, const std::string& str_); // full
static void swap(BigReal& l_val_, BigReal& r_val_);
static std::size_t& start_amount_digits_after_comma(); // lval, for all class, singleton?
~BigReal();
private:
static void append_leading_zeros(std::string& str_, std::size_t size_);
static void append_leading_zeros_by_size(std::string& str_, std::size_t size_);
static void append_leading_zeros_by_general_size(std::string& l_val_, std::string& r_val_);
static void remove_leading_zeros(std::string& str_);
static void remove_leading_zeros_from_whole_part(BigReal& val_);
static void append_end_zeros(std::string& str_, std::size_t size_);
static void append_end_zeros_by_size(std::string& str_, std::size_t size_);
static void append_end_zeros_by_general_size(std::string& l_val_, std::string& r_val_);
static void append_end_zeros_by_general_size(BigInt& l_val_, BigInt& r_val_);
static void remove_end_zeros(std::string& str_);
static void remove_end_zeros_from_fractional_part(BigReal& val_);
static void to_big_real_whole_part(BigReal &br_, std::string str_); // edit whole part
static void to_big_real_fractional_part(BigReal &br_, std::string str_); // edit fractional part
static BigReal to_big_real_from_big_int(const BigInt& wp_val_, const BigInt& fp_val_); // only positive
static bool is_carry_after_addition_fractional_part(const BigInt& l_val_,
const BigInt& r_val_,
const BigInt& res_);
static BigInt remove_first_number_from_big_int(const BigInt& val_); // after carry
static void set_comma_in_result_string_after_multiplication(std::string& res_str_, std::size_t offset_for_com_);
static BigReal addition_positive_or_negative_numbers(const BigReal& l_val_, const BigReal& r_val_);
static BigReal subtraction(const BigReal& l_val_, const BigReal& r_val_); // unsigned, l - r
static BigReal subtraction_in_order(const BigReal& l_val_, const BigReal& r_val_); // more - less
// unsigned
static bool logical_more(const BigReal& l_val_, const BigReal& r_val_);
static bool logical_more_whole_part(const BigReal& l_val_, const BigReal& r_val_);
static bool logical_more_fractional_part(const BigReal& l_val_, const BigReal& r_val_);
static bool logical_less(BigReal l_val_, BigReal r_val_);
static bool logical_less_whole_part(BigReal l_val_, BigReal r_val_);
static bool logical_less_fractional_part(BigReal l_val_, BigReal r_val_);
static bool logical_equal(const BigReal& l_val_, const BigReal& r_val_);
static bool logical_equal_whole_part(const BigReal& l_val_, const BigReal& r_val_);
static bool logical_equal_fractional_part(const BigReal& l_val_, const BigReal& r_val_);
// from str
static BigReal read_big_real(const std::string& str_, std::size_t st_p_);
std::vector<std::int8_t> b_whole_part;
std::vector<std::int8_t> b_fractional_part;
std::size_t b_am_dig_after_com; // for cout, division...
bool b_is_neg;
};
}